Is case var(var x, var y) a valid syntax ?
brian.goetz at oracle.com
Tue Sep 8 13:53:36 UTC 2020
> - Is either total or partial. Deconstruction patterns are total; other patterns we'll be able to express later, such as `Optional.of(var x)`, are partial.
While I don’t want to get into a deep dive on declared patterns now, let’s just say that they’ll exist and they’re important (Optional.of(var x), Optional.empty(), etc.) So in increasing order of generality:
Type patterns: T t
Deconstruction patterns: T(var x)
Declared patterns: Optional.of(var x)
Remi would like to view deconstruction patterns as a generalization of type pattern, but they are more properly understood as a restriction of declared patterns — just as constructors can be understood as a restriction of methods. Because we haven’t seen much of declared patterns yet, it is easier to miss their role in the spectrum, but their role is pretty obvious once you step back and think about it.
Deconstruction patterns are constrained compared to declared patterns in:
- their name (like constructors!)
- They are total on their declaring class, so they are guaranteed to match (much like a constructor is guaranteed to not return null)
- While instance members, they are not inherited (just like constructors)
- They can only be called via a pattern match (just as a constructor can only be called via a `new` operation.)
The role of a constructor is to take an external state description, validate and normalize it, and produce an instance.
The role of a deconstructor is to take an instance and produce an external state description. (If the ctor has to copy mutable elements on the way in, the dtor is also likely to want to do the same on the way out.)
In this way, both ctor and dtor mediate access between an external API and the internal representation.
More information about the amber-spec-experts