user3451476
user3451476

Reputation: 307

Variations of parameters in Scala

I am new to Scala and would like to know the differences between the following

define testFunc(par1: String) =  { }

define testFunc(par1: String)(par2: String) = {}

define testFunc(par1: String)(implicit par2: String) = {}

Upvotes: 0

Views: 55

Answers (1)

jwvh
jwvh

Reputation: 51271

def testFunc(par1: String) =  { }

A function (actually a method) that takes a String as its only parameter.

def testFunc(par1: String)(par2: String) = {}

A function (method) that takes two parameters, both of type String. The parameters are curried.

Currying is interesting. If you have a function that takes two curried arguments, and you call that function with only the first argument, what you get back is a new function that takes the other argument.

def testFunc(par1: String)(implicit par2: String) = {}

Same as previous but if the 2nd parameter is not provided when the function is called then the compiler will look to see if a String that has been declared implicit is available. If only one implicit string is in scope then that will be used as the 2nd parameter.

Note that this changes the curried aspect of the function in that it is an error if the implicit is not found or supplied. You don't get the new function as before.

Upvotes: 2

Related Questions