Skip to content

Instantly share code, notes, and snippets.

@milessabin
Created March 20, 2018 10:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milessabin/5434cef5bb88545237e43f20abf96893 to your computer and use it in GitHub Desktop.
Save milessabin/5434cef5bb88545237e43f20abf96893 to your computer and use it in GitHub Desktop.
Side-effects are bad, right?
// What output does this yield? What should it? (try with either 2.12.x or 2.13.x)
// (spoiler: https://github.com/scala/bug/issues/10788)
object Test {
class Box[T](t: T) {
def foo: T = {
println("effect")
t
}
}
object Box {
def apply(x: String): Box[x.type] = new Box[x.type](x)
}
def main(args: Array[String]): Unit = {
val bar = Box("foo").foo
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment