how is function getting parameters?

54 views
Skip to first unread message

Ravikumar Maddi

unread,
Feb 10, 2017, 2:38:47 AM2/10/17
to scala-functional

I am new to Scala, I need to work on Scala existing project. I am getting basic doubts so you should bear to help me please:

Doubt:

I have one function in service class like this

def updateTimeStamp(id: String, timeStamp: Date)

in test cases, it saw the code like

service.updateTimeStamp _

I check already but there is no varibiables (id and timeStamp) defined even to feel auto binding might happen. Then how I am getting those two parameters. 

Could you clear my doubt please.  

pagoda_5b

unread,
Feb 10, 2017, 5:12:35 AM2/10/17
to scala-functional
the notation with the underscore following a method name, is used to create a function object that will execute the method when called with the parameters.

val funRef = service.updateTimeStamp _
 
means that now you can pass around the method as if it was a function, and you can use it with other higher order functions (functions that accept other functions as args, like List.map, filter, reduce and so on) 

pagoda_5b

unread,
Feb 10, 2017, 5:13:32 AM2/10/17
to scala-functional
I forgot to mention that you can then do

val updated = funRef("my string", new Date())

Ravikumar Maddi

unread,
Feb 10, 2017, 6:51:39 AM2/10/17
to scala-functional
So, I am assuming that it will find the parameter in scope with matching name then those will bind updateTimeStamp function and it will execute. Am i right? 

pagoda_5b

unread,
Feb 24, 2017, 6:45:14 AM2/24/17
to scala-functional
You're assuming that the function is called right there, instead funRef is just a variable holding a pointer to the updateTimeStamp function.

You can then pass the funRef variable around and decide to invoke the function later from the reference, supplying the values.

Is it more clear now?

Ivano

Babatunde Ekemode

unread,
Apr 26, 2017, 2:43:25 AM4/26/17
to scala-functional
From the little a can understand from your post. You can't figure out how id and timestamp is being passed to the service.updateTimeStamp function. There is a concept in scala called partially applied functions. Which i can only illustrate via example. Let say in your REPL you define a function like the following:

def fullName(firstName: String, lastName: String): String = firstName + " " + lastName

This is basically a function that takes two parameters of type String and returns a String - (String, String) => String. To call the function u've to pass these arguments, for example:

(Note: Apologies. I don't know which is your first name or last, bear with me, no offence intended.)

val yourFullName = fullName("Ravikumar", "Maddi")   which is equivalent to val yourFullName = "Ravikumar Maddi". 

There are other ways to use this function, for example let say you want to provide your first name first and provide your last name later. you do that like this:

val  firstNameGivenProvideLast = fullName("Ravikumar", _: String) this will evaluate to a function with the signature String => String, which means a function that takes a String n returns a String. firstNameGivenProvideLast is a function that contains value for the first name that was provided at definition, and it expects  a String to evaluate. eample of usage:

val newFullName = firstNameGivenProvideLast("Maddi") which evaluate to "Ravikumar Mandi" just like yourFullName variable above.

Similar to your code you can also do:

val partiallyAppled = fullName _ 

which evaluate to a function with the signature: (String, String) => String, which in this case is redundant because it still the same function as fullName. The beauty of partially applied functions can be seen in the previous example.

The concept of calling a function and omitting some parameters by providing them has _ - the wildcard symbol, means you're partially applying this function which is what is happening in your test case. So one way or the other arguments would be passed to this partial applied functions to execute them either you do that explicitly has illustrated above or your test library is doing that if you notice the service.updateTimeStamp body is being executed when you run your tests.



Reply all
Reply to author
Forward
0 new messages