PROPOSAL: 'final' without explicit type

Marek Kozieł develop4lasu at gmail.com
Sun Mar 29 14:22:40 PDT 2009


2009/3/29 Florian Weimer <fw at deneb.enyo.de>:
> * Reinier Zwitserloot:
>
>> final list = foo ? new LinkedList() : new ArrayList();
>>
>> the type of 'list' is what, exactly?
>
> It's the type specified in section 15.25 of the JLS.  I can't find a
> definition of lub(T1, T2) in the spec, but "lub" probably stands for
> "least upper bound", and lub(LinkedList, ArrayList) would be
> AbstractList & Serializable & Cloneable (if I got the types right).
>
>> Serializable? Cloneable? List?  They're all valid, so that wouldn't
>> work.
>
> Intersection types are already part of the language, so I don't see
> any problem.  The following compiles:
>
>    interface I1 {}
>    interface I2 {}
>
>    static class C1 implements I1, I2 {}
>    static class C2 implements I1, I2 {}
>
>    static <T extends I1 & I2> void foo1(T foo) {
>    }
>
>    static void foo1(boolean foo) {
>        foo1(foo ? new C1() : new C2());
>    }
>
> Existence of intersection types also leaks to the surface during
> overload resolution.
>
> It's just that you can't write down the type of some expressions using
> Java type notation.  For local variables, this isn't a problem; only
> debugging information needs to be updated slightly.  The effect for
> fields would be more pronounced, and I guess to stay within COIN's
> scope, a proposal would have to exclude inference for fields.
>

You got it wrong.
Java do not support intersection types, or we use different definition.

T extends I1 & I2 : not intersection
I1 & I2: intersection

	interface I1 {}
	
	interface I2 {}
	
	<T extends I1&I2,K extends I1&I2> void noInterection(T t,K k){
		if (boo)t=k; // Type mismatch: cannot convert from K to T
		...
	}
	
	void intersection( I1&I2 t,  I1&I2 k){
		if (boo)t=k; // OK
		I1 i1 = t; // OK
		I2 i2 = t; // OK
	}

http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#108433

"is not possible to write an intersection type directly as part of a
program; no syntax supports this."

-- 
Pozdrowionka. / Regards.
Lasu aka Marek Kozieł

http://lasu2string.blogspot.com/



More information about the coin-dev mailing list