You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In some cases the compiler becomes confused if you are binding a variable whose name is the same as a default argument in the expression being bound, as in (1) below.
// Setup
case class Foo[A,B](f: (A, B) => A)
case class Person(name:String)
// (1) Doesn't compile:
// REPL says value 'b' not found.
// Scala IDE says "error applying default arguments"
val name = Foo[Person,String]((a, b) => a.copy(name = b))
// (2) Ok -- val is not called 'name'
val wibble = Foo[Person,String]((a, b) => a.copy(name = b))
// (3) Ok -- type ascription on LHS
val name:Foo[Person,String] = Foo((a, b) => a.copy(name = b))
The text was updated successfully, but these errors were encountered:
@adriaanm said:
This now gives a nice error message explaining what's going on:
scala>caseclassFoo[A,B](f: (A, B) =>A)
defined classFoo
scala>caseclassPerson(name:String)
defined classPerson
scala>valname=Foo[Person,String]((a, b) => a.copy(name = b))
<console>:11:error: not found: value b
valname=Foo[Person,String]((a, b) => a.copy(name = b))
^
<console>:11:warning: type-checking the invocation of method copy checks if the named argument expression 'name= ...' is a valid assignment
in the current scope. The resulting typeinference error (see above) can be fixed by providing an explicit typein the local definition for name.
valname=Foo[Person,String]((a, b) => a.copy(name = b))
^
scala>valwibble=Foo[Person,String]((a, b) => a.copy(name = b))
wibble:Foo[Person,String] =Foo(<function2>)
scala>
scala>valwibble=Foo[Person,String]((a, b) => a.copy(name = b))
wibble:Foo[Person,String] =Foo(<function2>)
In some cases the compiler becomes confused if you are binding a variable whose name is the same as a default argument in the expression being bound, as in (1) below.
// Setup
case class Foo[A,B](f: (A, B) => A)
case class Person(name:String)
// (1) Doesn't compile:
// REPL says value 'b' not found.
// Scala IDE says "error applying default arguments"
val name = Foo[Person,String]((a, b) => a.copy(name = b))
// (2) Ok -- val is not called 'name'
val wibble = Foo[Person,String]((a, b) => a.copy(name = b))
// (3) Ok -- type ascription on LHS
val name:Foo[Person,String] = Foo((a, b) => a.copy(name = b))
The text was updated successfully, but these errors were encountered: