Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala - What does ' => SomeType' means? [duplicate]

Tags:

scala

Today I would like to ask what does the => SomeType mean. I found it used in this article. It's in the part 'The Sequential Combinator'.

Thanks for any answer!

like image 363
Arg Avatar asked Aug 29 '11 01:08

Arg


People also ask

What does this symbol => Do in Scala?

Show activity on this post. => is the "function arrow". It is used both in function type signatures as well as anonymous function terms. () => Unit is a shorthand for Function0[Unit] , which is the type of functions which take no arguments and return nothing useful (like void in other languages).

What does => int mean in Scala?

It means when you call the method, the argument is not evaluated before the method is executed, but rather, it is evaluated each time it is referred to inside the method.

What does ._1 mean in Scala?

_1 is: _ = wildcard parameter _1 = first parameter in method parameter list But when used together with .


1 Answers

It means a block of code that you can run.

So for example:

scala> def doTwice(op: =>Unit) = {op; op}
doTwice: (op: => Unit)Unit

scala> doTwice({println("Hi")})
Hi
Hi

In this case the =>Unit is {println("Hi")}

Here "SomeType" is Unit because println doesn't produce a value. If it produced an Int, it would be =>Int.

like image 58
Owen Avatar answered Oct 25 '22 16:10

Owen