Surbhi Jain
Surbhi Jain

Reputation: 369

Functions and function literals in scala

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 "=>"))

Upvotes: 2

Views: 138

Answers (1)

Kevin Meredith
Kevin Meredith

Reputation: 41939

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.

Upvotes: 4

Related Questions