Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

scala

scala-3

I cannot find explanation of the following syntax rule:

FunType ::= FunTypeArgs (‘=>’ | ‘?=>’) Type
like image 707
user3514920 Avatar asked Sep 11 '21 18:09

user3514920


People also ask

What is the meaning of => in Scala?

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

What does :+ mean in Scala?

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.

What is the difference between :: and #:: In Scala What is the difference between ::: and in Scala?

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.

What does Star mean in Scala?

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].


1 Answers

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

like image 119
Silvio Mayolo Avatar answered Oct 18 '22 22:10

Silvio Mayolo