I cannot find explanation of the following syntax rule:
FunType ::= FunTypeArgs (‘=>’ | ‘?=>’) Type
=> is syntactic sugar for creating instances of functions. Recall that every function in scala is an instance of a class. For example, the type Int => String , is equivalent to the type Function1[Int,String] i.e. a function that takes an argument of type Int and returns a String .
On Scala Collections there is usually :+ and +: . Both add an element to the collection. :+ appends +: prepends. A good reminder is, : is where the Collection goes. There is as well colA ++: colB to concat collections, where the : side collection determines the resulting type.
In general: :: - adds an element at the beginning of a list and returns a list with the added element. ::: - concatenates two lists and returns the concatenated list.
The _* type annotation is covered in "4.6. 2 Repeated Parameters" of the SLS. The last value parameter of a parameter section may be suffixed by “*”, e.g. (..., x:T ). The type of such a repeated parameter inside the method is then the sequence type scala.Seq[T].
?=>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With