Classes, specializations, and statics
Peter Levart
peter.levart at gmail.com
Wed Feb 17 17:03:17 UTC 2016
Hi Brian,
On 02/15/2016 07:11 PM, Brian Goetz wrote:
> Example:
>
> class Collection<any T> {
> private __SS Collection<T> emptyCollection = …
> // ACC_SS field emptyCollection : ParamType[Collection, TypeVar[T]]
>
> private __SS Collection<T> emptyCollection() { return
> emptyCollection; }
> ACC_SS emptyCollection()ParamType[Collection, TypeVar[T]] {
> getstatic ParamType[Collection, TypeVar[T]].emptyCollection :
> ParamType[Collection, TypeVar[T]]]
> areturn
> }
>
> When we specialize Collection<int>, the field type, method return
> type, etc, will all collapse to Collection<int> by the existing
> mechanisms.
This would work if the emptyCollection was actually empty and immutable,
but could you do the following:
class Collection<any T> {
private __SS Collection<T>collection = new ArrayList<T>();
public __SS Collection<T> collection() { return collection; }
}
And then in code:
Collection<String> cs = Collection<String>.collection();
Collection<Number> cn = Collection<Number>.collection();
cs.add("abc");
Number n = cn.iterator().next();
If cs and cn hold the same instance, we have introduced heap corruption
without compilation warnings.
So I suppose in language you could only access the _SS members in the
following way:
Collection<?>.collection();
Collection<int>.collection();
Collection<long>.collection();
Collection<ValueType>.collection();
...
but not:
Collection<Object>.collection();
Collection<String>.collection();
...
Like .class literals in the prototype.
Regards, Peter
More information about the valhalla-spec-observers
mailing list