andbin
04-10-2007, 10:26
Sto leggendo il JLS (Java Language Specification) e ci sono dei punti nella sezione §5.5 Casting Conversion (http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.5) che non capisco. So che sono cose molto particolari e specifiche ma se qualcuno è in grado di spiegarle, ringrazio in anticipo.
Ad un certo punto dice:
A cast from a type S to a parameterized type (§4.5) T is unchecked unless at least one of the following conditions hold:
* S <: T.
* All of the type arguments (§4.5.1) of T are unbounded wildcards.
* T <: S and S has no subtype X≠T , such that the erasures (§4.6) of X and T are the same.
I primi due punti mi sono molto chiari, il terzo (evidenziato in rosso) no.
In pratica dice che il cast non è unchecked se la condizione è che: T è un sottotipo di S (quindi è un down-cast) e S non ha un sottotipo X diverso da T tale che le erasure di X e T sono le stesse. Cioè?? :mbe:
Solo con la prima parte (T <: S) un esempio sarebbe facile da fare:
class Super { }
class Sub<T> extends Super { }
Super sup = new Sub<String> ();
Sub<String> sub = (Sub<String>) sup;
Qui Super è S e Sub<String> è T ma il cast è unchecked. E in quale condizione non sarebbe unchecked?? Un esempio?
---------------------------
Altro punto, sempre in §5.5 un po' più sotto:
The detailed rules for compile-time legality of a casting conversion of a value of compile-time reference type S to a compile-time reference type T are as follows:
* If S is a class type:
* If T is a class type, then either |S| <: |T|, or |T| <: |S|; otherwise a compile-time error occurs. Furthermore, if there exists a supertype X of T, and a supertype Y of S, such that both X and Y are provably distinct parameterized types (§4.5), and that the erasures of X and Y are the same, a compile-time error occurs.
Anche qui la prima parte mi è chiara, S e T devono essere in "linea di ereditarietà" (String e Integer ad esempio non lo sono). La parte successiva (evidenziata in rosso) no, non mi è chiara. Anche qui un esempio sarebbe utile.
Ad un certo punto dice:
A cast from a type S to a parameterized type (§4.5) T is unchecked unless at least one of the following conditions hold:
* S <: T.
* All of the type arguments (§4.5.1) of T are unbounded wildcards.
* T <: S and S has no subtype X≠T , such that the erasures (§4.6) of X and T are the same.
I primi due punti mi sono molto chiari, il terzo (evidenziato in rosso) no.
In pratica dice che il cast non è unchecked se la condizione è che: T è un sottotipo di S (quindi è un down-cast) e S non ha un sottotipo X diverso da T tale che le erasure di X e T sono le stesse. Cioè?? :mbe:
Solo con la prima parte (T <: S) un esempio sarebbe facile da fare:
class Super { }
class Sub<T> extends Super { }
Super sup = new Sub<String> ();
Sub<String> sub = (Sub<String>) sup;
Qui Super è S e Sub<String> è T ma il cast è unchecked. E in quale condizione non sarebbe unchecked?? Un esempio?
---------------------------
Altro punto, sempre in §5.5 un po' più sotto:
The detailed rules for compile-time legality of a casting conversion of a value of compile-time reference type S to a compile-time reference type T are as follows:
* If S is a class type:
* If T is a class type, then either |S| <: |T|, or |T| <: |S|; otherwise a compile-time error occurs. Furthermore, if there exists a supertype X of T, and a supertype Y of S, such that both X and Y are provably distinct parameterized types (§4.5), and that the erasures of X and Y are the same, a compile-time error occurs.
Anche qui la prima parte mi è chiara, S e T devono essere in "linea di ereditarietà" (String e Integer ad esempio non lo sono). La parte successiva (evidenziata in rosso) no, non mi è chiara. Anche qui un esempio sarebbe utile.