The translation is more or less the one of described in the document pattern-match-translation with the difference that it uses one carrier (the uber carrier) for the whole pattern matching instead of one by pattern.
- it supposes that the switch takes an Object as argument.
- you can have only one de-constructor by class (i feel lucky).
- the escape analysis fails because the result of invokedynamic acts as a join of several creations of the uber carrier, something the VM is not able to cope.
The uber carrier is a dynamically generated tuple, the class TupleHandle takes a MethodType (with void as return type) and provides a constructor method handle to create a tuple and a way to get a method handle for each components.
Internally the a tuple is erased as a concatenation of object fields and long fields represented by the class TupleHandle.Form. The idea is that for one form, there is one dynamically generated class, so every tuples derived from a form will uses the same class.
A Pattern is a Form and a lambda that construct a method handle tree that will match the pattern from the Form of the uber tuple. When the pattern is constructed, it calculates the Form that will contains all extractions.
>From the Form corresponding to the uber carrier, it is possible to as for a TupleHandle from a MethodType that map the tuple virtual field to the Form class actual field (and initialises the remaining fields to 0, false, etc).
The protocol uses to match an object is to add two fields, a selector and a boolean, to each TupleHandle, the selector is bound to the number of the case (and -1 for default) and the boolean indicate if the matching succeed or not.
By example, the class Point is defined like this.
The de-constructor is a method annotation with @Deconstruct. The value of @Deconstruct indicates the intended signature. When the MethodHandle tree is constructed , the MethodHandle taken as argument of the de-constructor is mapped to the constructor TupleHandle able to read the MethodType corresponding to the value of the de-constructor.
At that point, the selector index, is also bind, so it will be available after matching. if a tuple is constructed with true at first argument, it means that the pattern match, and the following arguments are the de-constructed value.
So in the example, if x equals y , the pattern does not match, otherwise it matches and returns the value of x as first argument and y as second argument.
- allow TupleHandle to see a part of the uber carrier that doesn't start at the beginning (to implement Pattern.and()).
> À: "Maurizio Cimadamore" <maurizio.cimadamore at oracle.com>, "amber-dev" <amber-dev at openjdk.java.net>, "Gavin Bierman"
>>> it seems that something is missing.
>>just after indy i the translation) can access to the carrier object.
>>Brian about the carrier object.
>>you can not do side effects if they use the classical switch syntax.
>>one for the classical switch and one for the expression switch.