Dave Prateek
Dave Prateek

Reputation: 81

What type of FunctionN instance is created when creating anonymous function with no parameter in Scala

I wrote the below scala code in my IDE(IntelliJ) to create an anonymous function :

val hi = () => "Hi"

When I desugar my scala code , IDE gives me code like one below :

val hi: Function1[(), _root_.java.lang.String] = () => "Hi"

But it is giving error : '=>' expected but ',' found.

  1. I am keen to know the Function type of the anonymous function.
  2. I also want to know why the IDE is giving me this result which is actually throwing error.

Upvotes: 0

Views: 65

Answers (1)

Gaël J
Gaël J

Reputation: 15295

As said by Luis, hi is a function of no input parameter that produces a String, thus its type is Function0[String].

If it was defined like this for instance it would be a Function1[String, String]:

val hi1 = (name: String) => s"Hi $name"

Upvotes: 1

Related Questions