Huston, we have a problem !

Ulf Zibis Ulf.Zibis at
Tue Feb 22 09:22:07 PST 2011

Am 18.02.2011 19:03, schrieb Neal Gafter:
> On Fri, Feb 18, 2011 at 9:10 AM, Florian Weimer<fweimer at>  wrote:
>> Clearly, those a different at compile time.  What I'm looking for is a
>> case where both Foo and Foo<>  result in legal expressions at compile
>> time and produce different byte code.
> *public class X<T>  {
>      public X(T t) {}
>      public T get() { return null; }
>      public static int f(String s) { return 1; }
>      public static int f(Object o) { return 2; }
>      public static void main(String[] args)
>      {
>          System.out.println(f(new X<>("").get()));
>          System.out.println(f(new X("").get()));
>      }
> }*

1.) Yes, the diamond operator is helpful to explicitly target overlaid methods. But the term 'new 
X<>("")' exactly creates the same object than 'new X("")' and <> is more used as a way to describe a 
cast outside that term than the way of instantiating an object. We could deliberately code 
f((String)(new X("").get())) to result in 1, which would be much more descriptive, even for long 
experienced programmers, and avoid misinterpretation, confusion and 
too-much-too-learn-before-first-success at least for beginners.

2.) It doesn't even help if there are 3 overloadings:
     public static int f(Object o) { return 1; }
     public static int f(Number n) { return 2; }
     public static int f(Integer i) { return 3; }

I personally would prefer, that 'f(new X(1).get())' would invoke f(Integer i) by default. If someone 
wants to rollback to Number or Object, he should use an explicit cast. But this may be impossible 
for compatibility reasons.

3.a) Does it help here? :
         X a = new X<>("");
         X b = new X("");

3.b) Or here? :
         X<?> a = new X<>("");
         X<?> b = new X("");

So I think, the question of Florian is again kinda open.


More information about the coin-dev mailing list