Proposal To Revise Implicit Parameters

Maybe we should try to frame this discussion by mentioning the downsides of the current syntax that we’d like to address, here’s a first attempt:

  1. The current syntax has a concept of implicit parameter list at definition-site, but does not distinguish implicit and explicit parameter lists at use-site. This means it’s easy to accidentally pass an argument to an implicit parameter (in fact this has happened at least once in Dotty itself).
  2. The current syntax does not allow having explicit parameter lists after implicit parameter lists
  3. The current syntax does not allow anonymous implicit parameters (except by using context bounds)

I think the most straight-forward way to address 1. is to keep the definition-site syntax the same but require mirroring it at use-site, e.g.:

def minimum[T](xs: List[T])(implicit ord: Ord[T]) =
  maximum(xs)(implicit descending)

minimum(xs)
maximum(xs)(implicit descending)
maximum(xs)(implicit descending(implicit ListOrd))
maximum(xs)(implicit descending(implicit ListOrd(implicit IntOrd)))

(I’m using implicit here since this is the current syntax, I think we should defer discussing keyword renaming for now since this isn’t central to the main discussion).

Once 1. is address then we can just remove the restriction from 2. which isn’t necessary anymore, however this can only be done once all the existing code has been updated to use the new syntax at use-site, so that means a somewhat long transition period.

Finally, 3. could be addressed by allowing something like this:

def minimum[T](xs: List[T])(implicit Ord[T]) =
  maximum(xs)(implicit descending)

implicit _: Ord[T] could also work but I’d rather not add more uses of underscores in the language. In any case this seems like something that could be discussed separately as its own proposal.

Compared to the original proposal in this thread this has several advantages I think:

  • No existing definition needs to be rewritten
  • The use-site syntax does not require spending precious brain-cycles thinking about where to put parentheses to please the parser.
  • This is a less radical change, so existing users are less likely to see the new syntax as alien and then declare that “Scala 3 is a new language” which at the very least is bad PR I think.

Opinions?

14 Likes