Question on layer/peeling

Brian Goetz brian.goetz at
Tue Jan 6 02:21:56 UTC 2015

(Public service announcement: Any talk about "spec" is woefully 
premature; what we've got is an idea on a white board.)

The case you describe is what we call "implementation by parts", where 
there is a method who is a member of Foo<any T> but which might have 
different implementation depending on whether T is a value or an 
(erased) reference.  This is one of the use cases described in SotS, and 
such things come up in a number of places inside the implementation of 

While we care little about syntax right now, what you wrote is indeed in 
the spirit of the idea, and how you would address an "implementation by 
parts" in that situation.

Implementation-wise, this is pretty straightforward with the existing 
specialization story.  The compiler emits two methods with some 
"applicability" attributes, and the specializer selects which to 
propagate into the specialized classfile.

On 1/5/2015 9:14 PM, Michael Barker wrote:
> Hi,
> The SotS talks about the use of 'layer' to create an alternative
> implementation of methods when the type of an <any T> is known to be a
> reference type.  However, the examples only show the use of the layer
> keyword on an interface definition, where as I've encountered at least one
> case where the internal implementation needs to differentiate between a
> reference-type and value-type based collection.  The example I'm thinking
> about is the null-ing out of array elements in a collection (which is
> obviously a no-op with a value type, but necessity with reference
> types).  Is an interface required in order to define a 'layer' or could it
> be done within a concrete class?
> E.g. is the following or something similar possible?  If not, how would it
> be achieved with current spec?
> class ArrayList<any T> {
>      T[] values;
>      int position;
>      void removeLast() {
>          if (position <= 0) {
>              return;
>          }
>          --position;
>          clear(position);
>      }
>      private void clear(int index) {
>      }
>      layer<ref T> {
>          private void clear(int index) {
>              values[index] = null;
>          }
>      }
> }
> Mike.

More information about the valhalla-dev mailing list