Huston, we have a problem !
Ulf Zibis
Ulf.Zibis at gmx.de
Sun Mar 6 11:41:59 PST 2011
Am 06.03.2011 12:23, schrieb "Zdeněk Troníček":
> class Foo<X> {
> Foo(X x) { }
> Foo<X> get(X x) { return this; }
> }
>
> class Test {
> void test() {
> Foo<?> f1 = new Foo(1).get(""); //ok - can pass String where Object
> is expected
> Foo<?> f2 = new Foo<>(1).get(""); //fail - cannot pass String where
> Integer is expected
> }
> }
>
> This shows the difference between new Foo(1) and new Foo<>(1). Clearly,
> these two are different and if we changed the semantics of new Foo(1), it
> would break backward compatibility. But wait. Backward compatibility with
> what? Isn't line
> Foo<?> f1 = new Foo(1).get("");
>
> a little suspicious? It uses generic type in the left part and raw type in
> the right part. Although it is legal, it is probably either omission or
> malpractice. And its legality is probably only a side effect of "a
> concession to compatibility of legacy code".
I agree!
> public class X {
> public X(Object o) { }
> public Object get() { return null; }
> }
>
> and some code that compiled against this library:
>
> public class Client {
> static int f(String s) { return 1; }
> static int f(Object o) { return 2; }
>
> public static void main(String[] args) {
> System.out.println(f(new X("").get()));
> }
> }
>
> Then, the library was generified:
>
> public class X<T> {
> public X(T t) { }
> public T get() { return null; }
> }
>
> and we compiled the client project against the generified version. Now, if
> we changed the semantics of new X("") to new X<String>("") (or new X<>("")
> with the diamond syntax), the code would behave differently. So, the
> answer to the title question is 'yes'. If we want to stay backward
> compatible, we need<> and we cannot put new X("") semantically equal to
> new X<>("").
You are right, but we could use in place of new X<>(""):
(X<String>)new X("")
or:
(String)new X("").get()
As the only purpose of the "diamond syntax" here is to determine one from the overloaded methods,
IMHO the "cast syntax" 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.
We anyway *don't have any other choice* in case of:
X x = new X("");
System.out.println(f((String)x.get()));
... or if we have more than 2 overloaded f(...) methods !!!
So again, I don't understand, why we need the diamond operator. I think, we only need to allow
without warning:
Foo<?> f1 = new Foo(1).get("");
and:
Foo<String> f1 = new Foo(1).get("");
-Ulf
More information about the coin-dev
mailing list