Skip to content

Instantly share code, notes, and snippets.

@matthughes
Last active December 14, 2015 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthughes/5096112 to your computer and use it in GitHub Desktop.
Save matthughes/5096112 to your computer and use it in GitHub Desktop.
Uninitialized AnyVals sneaking past your validation
final class Foo private (val value: Int) extends AnyVal {
override def toString = value.toString
}
object Foo {
def apply(value: Int) = {
assert(value > 0)
new Foo(value)
}
}
val x: Foo = _
// scala> x = 0
case class FooBar(foo: Foo)
class UninitializedAnyValsInConstructor {
val bar: FooBar = FooBar(x)
val x: Foo = Foo(1)
}
val y = new UninitializedAnyValsInConstructor
// y.bar == FooBar(Foo(0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment