Skip to content

Instantly share code, notes, and snippets.

@vidma
Last active November 4, 2015 09:23
Show Gist options
  • Save vidma/ad6735fffdbdf89a8f1d to your computer and use it in GitHub Desktop.
Save vidma/ad6735fffdbdf89a8f1d to your computer and use it in GitHub Desktop.
trait T {
val v = 3
// if v is overriden, and this is val, this yields not what you'd expect
// lazy val or def would work ok.
val version = v
}
class C extends T {
override val v = 2
}
(new C).version
// get unexpected: Map(3 -> 3, 2 -> 2)
// version two
trait T {
var metricsData: Map[Int, Int] = Map()
// missing lazy here
val v = add(3)
def add(v: Int) = { metricsData += v -> v; v }
val version = v
}
class C extends T {
override val v = add(2)
}
(new C).metricsData
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment