1

This curried version works fine. Link to Scastie View code in action

def foo(a: Int)(b: Int = a-1): Int = a + b
foo(9)()

But this version throws the Error not found value a. Link to Scastie. View code in action

def bar(a: Int, b: Int = a-1): Int = a+b
bar(9)

Is this an actual error or is there any reason why it is not working?

1
  • 1
    Parameters are evaluated as they are grouped. The foo works because the 1st group, i.e. the a, is evaluated before the 2nd group, the b. The bar won't compile because a hasn't been evaluated before it is referenced, a-1. Commented Feb 18, 2018 at 4:16

1 Answer 1

2

Although parameters are evaluated sequentially (and before the body of the function), bar fails because a is not in scope inside its own params list.

The scope of a parameter is only public to the subsequent params list.

def foo(a: Int)(b: Int = a - 1)(c: Int = b + a) = a + b + c 
foo(9)()()
// 34
Sign up to request clarification or add additional context in comments.

Comments

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.