Functions and function literals in scala

133 Views Asked by At

I am new to Scala. Please tell the difference between

    def fun( t: Int => Int):Unit = {

and

    def fun(t: =>Int):Unit {

and

    def fun(t:=>Int):Unit { (without space b/w ":" and "=>"))
1

There are 1 best solutions below

4
Kevin Meredith On

def fun( t: Int => Int):Unit is a method that takes a single argument, t. Its type, Int => Int, is a function that takes an Int, and returns an Int. However, the return type of fun is Unit.

def fun(t: =>Int):Unit is a method that accepts a call by name argument t. Again, this method's return type is Unit.

See What is "Call By Name"? too.

There's no difference between the second and third methods.