Skip to content

Instantly share code, notes, and snippets.

@drostron
Last active December 19, 2015 13:29
Show Gist options
  • Select an option

  • Save drostron/5962943 to your computer and use it in GitHub Desktop.

Select an option

Save drostron/5962943 to your computer and use it in GitHub Desktop.
object Common {
trait I[A, B] {
def apply(a: A): B
}
def i[A, B](f: A => B): I[A, B] = new I[A,B] {
def apply(a: A) = f(a)
}
case class Z(boz: String)
}
import Common._
object WorkingCase {
val bozy: I[String, Z] = i(newBoz => Z(boz = newBoz))
val boz = bozy("zob")
}
object BuggyCase {
val bozy = i[String, Z](newBoz => Z(boz = newBoz))
val boz = bozy("zob") // fails?!
}
@tlockney
Copy link
Copy Markdown

This works fine for me under Scala 2.10.2 if you leave off the named argument ("boz") when calling Z.apply in each case. If you leave it in, it generates an interesting error:

/Users/thomas/tmp/Foo.scala:24: error: not found: value newBoz
  val bozy = i[String, Z](newBoz => Z(boz = newBoz))
                                            ^
/Users/thomas/tmp/Foo.scala:24: warning: type-checking the invocation of method apply checks if the named argument expression 'boz = ...' 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 boz.
  val bozy = i[String, Z](newBoz => Z(boz = newBoz))
                                          ^
one warning found
one error found

@drostron
Copy link
Copy Markdown
Author

Correct. Seems to be something to do with a named parameter clashing with the assignment variable name in this particular case. Strange.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment