Classes, specializations, and statics

Ali Ebrahimi ali.ebrahimi1781 at gmail.com
Tue Feb 16 08:10:21 UTC 2016


Hi,

On Tue, Feb 16, 2016 at 1:33 AM, Remi Forax <forax at univ-mlv.fr> wrote:

> The other solution is to allow static field to be parameterized like
> methods.
>
>   private static final <any T> Collection<T> EMPTY = new
> EmptyCollection<T>();
>
>   public static <any T> Collection<T> empty() {
>     return EMPTY;
>   }
>
> or perhaps it's just a different syntax of the same solution.

If you mean this:
private static final  Collection<any> EMPTY = new EmptyCollection<>();

This does not work: Collection<int> c = Collections.<int>empty(); //CCE

OR if you mean static field overloading :
private static final <any T> Collection<T> EMPTY = new EmptyCollection<T>();

equivalents to:

private static final  Collection EMPTY = new EmptyCollection<>();
private static final  Collection<int> EMPTY = new EmptyCollection<int>();
private static final  Collection<long> EMPTY = new EmptyCollection<long>();
...
for all val types...

I don't know how this can be cleanly handled.

One possible way: transfer empty field to EmptyCollection<any T>

  public static <any T> Collection<T> empty() {
    return EmptyCollection<T>.EMPTY;
  }

But this would work if EMPTY is private and not referenced from outside: So
we can not move that
public static final  Collection EMPTY = new EmptyCollection<>();


I think there is enough weapons here:

public static final  Collection EMPTY = new EmptyCollection<>(); //don't
touch

  public static <any T:where ref T> Collection<T> empty() {
    return EMPTY;
  }

  public static <any T:where val T> Collection<T> empty() {
    return EmptyCollection<T>.EMPTY;
  }

-- 

Best Regards,
Ali Ebrahimi


More information about the valhalla-spec-observers mailing list