Krishnadas PC
Krishnadas PC

Reputation: 6519

Error not Found value in function param which references other function param

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?

Upvotes: 1

Views: 235

Answers (1)

user3097405
user3097405

Reputation: 823

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

Upvotes: 2

Related Questions