Description
Motivation
The current syntax for implicit parameters has several shortcomings.
-
There can be only one implicit parameter section and it has to come at the end. Therefore
normal and implicit parameter types cannot depend on other implicit parameters except
by nesting in an inner object with anapply
method. -
The syntax
(implicit x: T, y: U)
is a bit strange in thatimplicit
conceptually scopes over
x
andy
but looks like a modifier for justx
. -
Passing explicit arguments to implicit parameters is written like normal application. This
clashes with elision ofapply
methods. For instance, if you havedef f(implicit x: C): A => B
then
f(a)
would pass the argumenta
to the implicit parameter and one has to write
f.apply(a)
to applyf
to a regular argument.
Proposal
-
Introduce a new symbolic delimiter,
?(
. This is one token, no spaces allowed between the?
and the(
. -
Write implicit parameter definitions with
?(
instead of(implicit
. E.g.def f(x: Int)?(ctx: Context) = ...
instead of
def f(x: Int)(implicit ctx: Context) = ...
-
Explicit arguments to implicit parameters have to be enclosed in
?(...)
. E.g.f(3)?(ctx)
-
There can be several implicit parameter sections and they can be mixed with normal parameter sections. E.g.
def f ?(ctx: Context)(tree: ctx.Expr) = ...
Problems
?
can be already used as an infix operator. So the meaning of(a)?(b)
would change but
only if no space is written after the?
, which should be rare.?
can be part of a symbolic operator. In this case the longest match rule applies. So
def #?(x: T)
defines an operator#?
with a regular parameter list. To define#
with
an implicit parameter list an additional space is needed:def # ?(x: T)
.