10

I cannot find explanation of the following syntax rule:

FunType ::= FunTypeArgs (‘=>’ | ‘?=>’) Type
user
  • 7,435
  • 3
  • 14
  • 44
user3514920
  • 141
  • 1
  • 3

1 Answers1

12

?=> denotes a context function type.

Context functions are written using ?=> as the “arrow” sign. They are applied to synthesized arguments, in the same way methods with context parameters are applied. For instance:

given ec: ExecutionContext = ...

def f(x: Int): ExecutionContext ?=> Int = ...

...

f(2)(using ec)   // explicit argument
f(2)             // argument is inferred

So, if you think of A => B as being analogous to

def foo(a: A): B

Then you should think of A ?=> B as being analogous to

def foo(using a: A): B

It's just like a regular function except that the argument is taken as a context parameter. You can refuse to supply it (and it will be inferred from all of the givens in-scope, similar to implicit in Scala 2), or you can explicitly supply it using the using keyword.

Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116