Comments/Feature Requests regarding JEP 305: Pattern Matching
Remi Forax
forax at univ-mlv.fr
Thu Jul 6 14:39:30 UTC 2017
----- Mail original -----
> De: "William Shackleford" <wshackle at gmail.com>
> À: amber-dev at openjdk.java.net
> Envoyé: Mercredi 5 Juillet 2017 22:34:07
> Objet: Comments/Feature Requests regarding JEP 305: Pattern Matching
[...]
>
> Also :
>
> int eval(Node n) {
> switch(n) {
> case IntNode(int i): return i;
> case NegNode(Node n): return -eval(n);
> case AddNode(Node left, Node right): return eval(left) +
> eval(right);
> case MulNode(Node left, Node right): return eval(left) *
> eval(right);
> default: throw new IllegalStateException(n);
> };
> }
>
>
>
> could be written more concisely as
>
>
> int eval(Node n) {
> switch(n) {
> case IntNode: return n.i;
> case NegNode: return -eval(n.n);
> case AddNode: return eval(n.left) + eval(n.right);
> case MulNode: return eval(n.left) * eval(n.right);
> default: throw new IllegalStateException(n);
> };
> }
The main issue with your example is that you have lost the encapsulation,
a pattern like
case AddNode(Node left, Node right)
said that you can extract two nodes from AddNode but you have not idea if left and right are stored as field or not in AddNode.
There is a nice paper by Ordersky and al. on that subject [1].
>
> -- Will
regards,
Rémi
[1] https://infoscience.epfl.ch/record/98468/files/MatchingObjectsWithPatterns-TR.pdf
More information about the amber-dev
mailing list