Took a look. Thanks!
I use named givens all the time (just did an hour ago), the new as
syntax for named givens is quite bad… Current syntax is much better.
From a learner’s perspective, the “simple alias” use case is the earliest and the most common. as
will have a cognitive recall of things like asInstanceOf
which suggests some sort of “casting” or “transformation” rather than “naming.” Whereas, current syntax is completely regular and in line with how normal val
s and def
s are written:
// good for learners
// keyword name: type = expression
given jolts: Jolts = getJolts(lines)
This is not good:
// Simple alias
// bad for learners
// keyword type `as` name = expression
given Ord[Int] as intOrd = IntOrd() // massive irregularity
- The infix
:
also feels strange. To a newcomer, it’s not clear what it signifies.
I totally disagree with this. :
is extremely familiar and helpful for learners.
The proposal seems, admittedly, biased heavily in favor of anonymous givens:
Arguably anonymous is the more important case. …
I think this focus is unfair: it says with
is an irregularity, but then that irregularity is pushed from anonymous givens to named givens. Why?
Again from a learner’s perspective, the “removing repeated parameters that never change with using
” use-case of givens comes much earlier than type classes. This is a much easier way to “ease” learners into the idea of “context”.
When givens are used along with a matching using
clause, we often have to use named givens, because they have to be referenced in multiple places in the same code; anonymous givens are not an option. Here’s an example from a Fibonacci-like Advent of Code puzzle with memoization I just solved, it was a perfect match for this use case:
private def paths(jolt: Jolt, index: Int)(using memo: Memo, jolts: Jolts): Jolt =
memo.get((jolt, index)) match
// ...
// ...
def solve2(lines: Seq[String]) =
given jolts: Jolts = getJolts(lines) // name needed for below
given Memo = collection.mutable.Map((jolts.last, jolts.size - 1) -> 1L)
paths(jolts.head, 0)
The proposal does improve things for type classes, but “removing repeated parameters with using
” is getting very poor treatment in my opinion. Named givens seem to be getting the short end of the stick here (having a big irregularity shoveled on them), and I’m against it.
If there has to be an irregularity somewhere, it makes more sense to have it for type classes, because they are fairly advanced and special. That’s much better from a learner’s perspective. Cognitively, this justifies the irregularity in the learner’s mind. I remember explaining to a learner in Coursera Discussion Forums a few months ago, with with
and extension
together, and they grasped it fairly easily.
Just my two cents.