Reputation: 2250
Ok, this is is not another question about fundamental differences between vals & defs or functions and methods. I see that while this compiles:
val Extractor = new AnyRef { def unapply(s :String) => Some(s) }
val x = "hello" match { case Extractor(s) => s }
changing val Extractor
to def Extractor
breaks the code. Why is it so? It's a bit disappointing, as I hoped for complete trasnparency that would let me change the implementation from vals (generating methods anyway) to defs, or vice versa. I wonder what else can be done with one but not another?
Upvotes: 2
Views: 213
Reputation: 167901
I'm not sure I can supply an exhaustive list, but I can explain what's going on in this case.
Extractor
has to be a stable identifier. See Section 8.1.8 of the Scala Language Specification. (A def
is not stable; a val
is.)
Stable identifiers have certain nice properties that allow them to be more simply computed and have greater optimization possibilities.
Upvotes: 4