“The” as a method?
Today in Interesting Dotty Experiments comes this PR. It’s exemplified in this test code:
trait Foo { type T; val x: T } implied intFoo for Foo {
type T = Int
val x = 3
} val y: Int = the[Foo].x
Translating that: Foo
is a trait, and intFoo
is the instance of Foo
. (More typically, Foo
would be a typeclass, and intFoo
would be the instance for Foo[Int]
, but this simplified case is mainly focused on the last line.) Since we have one instance of Foo
in scope, that we’re concentrating on, we simply refer to it as the[Foo]
.
So basically, the the
method replaces implicitly
. It’s part of an overall push to move away from focusing on implicit-the-mechanism and instead putting the spotlight on the semantic intent of what it’s accomplishing.
I’m still pondering this one, but from a plain-English POV I rather like it: it’s short, sweet, and really does mean what it says. The definite article “the” implies that there is only one thing it could reasonably be referring to, which is the intent here.
Usual caveat: this is a brand-new experiment (this morning, as I write this), so is as far from set-in-stone as you get. Comments and emoji should go on the PR itself…