Skip to content

New implicit parameter & argument syntax #1260

Closed
@odersky

Description

@odersky

Motivation

The current syntax for implicit parameters has several shortcomings.

  1. 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 an apply method.

  2. The syntax (implicit x: T, y: U) is a bit strange in that implicit conceptually scopes over
    x and y but looks like a modifier for just x.

  3. Passing explicit arguments to implicit parameters is written like normal application. This
    clashes with elision of apply methods. For instance, if you have

      def f(implicit x: C): A => B
    

    then f(a) would pass the argument a to the implicit parameter and one has to write
    f.apply(a) to apply f 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).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions