Anton Kim Anton Kim -4 years ago 156
Scala Question

Scala - Method Parameters - def function: Row => Message = { row => {

Coming from Java background trying to understand this Scala code:

def function: Row => Message = {
row => {
// code
// code
}
}


As I understand we pass a function that returns type Message? And then we actually implement row? Why first Row is capital and second is not?

Thanks.

Answer Source

Let's break it down.

def function

Declare a method named function that has no inputs.

:Row => Message

The return type is a function that take a Row as input and returns a Message

= row => {...}

Define and anonymous function with a single input named row. This is the function that is returned (in Scala the last thing in a block is returned so you don't need to use the return keyword). Scala is able to figure out what the input and output types of this function should be because they have to match the return type you declared for the method.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download