hg: lambda/lambda/jdk: Update initial lambda APIs and tests to use new syntax.

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Mon Sep 12 09:14:54 PDT 2011


On 12/09/11 16:28, Colin Decker wrote:
> On Sun, Sep 11, 2011 at 7:52 AM, Rémi Forax<forax at univ-mlv.fr>  wrote:
>> - some casts are wrong, you can't  cast a Predicate<? super T>  to
>>    a Predicate<T>, it's not safe.
>>
> What isn't safe about it? If it weren't safe, it wouldn't be safe to cast
> Predicate<Object>s to Predicate<T>  as you later advocate.
 From a language perspective, both casts are unsafe.

i.e.

class Foo<X> {
    Foo(X x)  { this.x = x);
    X x;
}

Foo<? super Number> fn = new Foo<Object>("");
Foo<Number> fnn = (Foo<Number>)fn;
Number n = fnn.x; //runtime class cast exception


Similar problem happens with Foo<Object> -> Foo<T> case:


<Z> Foo<Z> unsafe() { return (Foo<Z>)new Foo<Object>(""); }

Foo<Number> fnn = unsafe();
Number n = fnn.x; //runtime class cast exception


In the case of predicate however, since predicate consumes values of 
type T, it is not possible to reproduce the problem - given the shape of 
Predicate, a Predicate<Integer> is always 'stricter' than a Predicate<? 
super Integer> (i.e. the arguments passed to the first can safely be 
passed to the second). As such the cast can be considered as 
pseudo-safe, even though, strictly speaking, it is not (thanks to the 
fact that the type parameter of Predicate only occurs in a contravariant 
position).

Maurizio
>
>> - some methods return a Predicate<Object>  instead of a
>>    Predicate<T>  so it will not work if the predicate is used by example
>>    in a ?:
>>
> It isn't a bug for the methods to return the actual type of the predicate
> and I don't recall having seen any valid arguments in the last discussion as
> to why they should return Predicate<T>.
>



More information about the lambda-dev mailing list