What is the type of a lambda expression in Scala

Viewed 49

Say I had a lambda expression something like: val ex = (x:Int) => x + x what is the type of this expression/how do I know its type? I've tried using "Any" as the function parameter type that didn't work and if I try to get it's type using the .getClass.getSimpleName function I get an object address.

1 Answers

The type of this expression is a function accepting Int and returning Int, i.e. Int => Int (which is a syntactic sugar for Function1[Int, Int]):

val ex: Int => Int = (x:Int) => x + x

If you are using IntelliJ IDEA with Scala plugin you can use Alt+= shortcut to see the type info:

enter image description here

Related