Last active
December 14, 2015 13:48
-
-
Save matthughes/5096112 to your computer and use it in GitHub Desktop.
Uninitialized AnyVals sneaking past your validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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