Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler fails when applying default argument with the same name as variable being bound #6867

Closed
scabug opened this issue Dec 7, 2011 · 3 comments
Assignees
Milestone

Comments

@scabug
Copy link

scabug commented Dec 7, 2011

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))

@scabug
Copy link
Author

scabug commented Dec 7, 2011

Imported From: https://issues.scala-lang.org/browse/SI-6867?orig=1
Reporter: Rob Norris (rnorris)

@scabug
Copy link
Author

scabug commented Dec 7, 2011

Rob Norris (rnorris) said:
Perhaps this is in the wrong project. I assume someone will move it if that's the case.

@scabug
Copy link
Author

scabug commented May 16, 2014

@adriaanm said:
This now gives a nice error message explaining what's going on:

scala> case class Foo[A,B](f: (A, B) => A)
defined class Foo

scala> case class Person(name:String)
defined class Person

scala> val name = Foo[Person,String]((a, b) => a.copy(name = b))
<console>:11: error: not found: value b
       val name = 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 type inference error (see above) can be fixed by providing an explicit type in the local definition for name.
       val name = Foo[Person,String]((a, b) => a.copy(name = b))
                                                           ^

scala> val wibble = Foo[Person,String]((a, b) => a.copy(name = b))
wibble: Foo[Person,String] = Foo(<function2>)

scala> 

scala> val wibble = Foo[Person,String]((a, b) => a.copy(name = b))
wibble: Foo[Person,String] = Foo(<function2>)

@scabug scabug closed this as completed May 16, 2014
@scabug scabug added this to the 2.11.0 milestone Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants