Question on layer/peeling

Vitaly Davidovich vitalyd at gmail.com
Tue Jan 6 02:19:45 UTC 2015


C# has a default (T) keyword to allow generic code to obtain the "zero"
value for a type param.  Something like that for java would be nice.

Sent from my phone
On Jan 5, 2015 9:15 PM, "Michael Barker" <mikeb01 at gmail.com> 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