What is the meaning of "?=>" in Scala 3?

I cannot find explanation of the following syntax rule:

FunType ::= FunTypeArgs (‘=>’ | ‘?=>’) Type

1 answer

  • answered 2021-09-11 18:21 Silvio Mayolo

    ?=> 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.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum