1

I came across following type of function declaration:

def fun(): Int => Int => Int = { ..... }

How to interpret the type of function fun?

Which one of the following two interpretations it indicates?

First interpretation: fun is a function returning some function X which accepts another function Y as a parameter. This function Y accepts one integer and returns one integer ; and ultimately the function X returns an integer.

def fun(): (Int => Int) => Int = { ..... }

Second interpretation: fun is a function returning some function X which accepts an integer as a parameter and returns another function Y. This function Y accepts an integer and returns an integer.

def fun(): Int => (Int => Int) = { ..... }

So which one of these interpretations apply in this case?

2
  • 1
    It's quite easy to "ask" the scala compiler this question. Maybe you can figure out how? Commented Jul 6, 2015 at 13:50
  • Your second interpretation is almost correct, except for the fact that fun is a method, not a function. So, fun is a method with one empty parameter list which returns a function that takes an Int which returns a function that takes an Int and returns an Int. Commented Jul 6, 2015 at 15:00

1 Answer 1

2

fun() is a parameterless method that returns a function with one Int argument, that returns another function with one Int argument, that returns Int.

i.e. your second interpretation:

def fun(): Int => (Int => Int) = ???

=> in Scala is right associative, so that operation is grouped from right to left.

Sign up to request clarification or add additional context in comments.

1 Comment

You touched on the distinction between function and method in your explanation, but if you want to be really pedantic you also have to account for the difference between a method with zero parameter lists, and a method with one parameter list of zero parameters. "Parameterless method" is somewhat ambiguous in Scala, is that def meth (a method with zero parameter lists) or def meth() (a method with one parameter list of zero parameters)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.