[weld-dev] Odd type assignability test

Martin Kouba mkouba at redhat.com
Mon Apr 27 08:38:50 EDT 2020


Dne 27. 04. 20 v 14:26 Matej Novotny napsal(a):
> Forwarding this email to weld-dev to make it public
> 
> 
> So, the Foo<String> is the required parameterized type here and Foo<E> is parameterized bean type.
> 
> And from CDI spec[1]:
>> A parameterized bean type is considered assignable to a parameterized required type if they have identical raw type and for each parameter:
>> * the required type parameter is an actual type, the bean type parameter is a type variable and the actual type is assignable to the upper bound, if any, of the type variable, or
> 
> Isn't that the same case? Or am I just running low on caffeine? :)

I think that you're right. Foo<E> is basically Foo<E extends Object> and 
according to the rules String is assignable to Object.

> 
> Matej
> 
> _________________________________________________________________________________
> [1] https://docs.jboss.org/cdi/spec/2.0/cdi-spec.html#assignable_parameters
> 
> ----- Original Message -----
>> From: "Laird Nelson" <ljnelson at gmail.com>
>> To: "Matej Novotny" <manovotn at redhat.com>
>> Sent: Sunday, April 26, 2020 10:52:57 PM
>> Subject: Odd type assignability test
>>
>> Hello; I ran across this test during some code reading to better understand
>> type assignability.  It's in BeanTypeAssignabilityTest.java:
>>
>> @Test
>> public <E> void testStringFooMatchesVariableFoo() throws Exception {
>>      Type stringFooType = new TypeLiteral<Foo<String>>() {
>>      }.getType();
>>      Type variableFooType = new TypeLiteral<Foo<E>>() {
>>      }.getType();
>>      Assert.assertTrue("Foo<String> should match Foo<E>",
>> getRules().matches(stringFooType, variableFooType));
>> }
>>
>> Am I reading this right?  Is this saying that a reference of type
>> Foo<String> should be able to receive a Foo<E>, where E doesn't extend
>> String?
>>
>> I understand that any method that declares a type variable E and returns a
>> Foo<E> will happen to produce a compatible reference.  That is:
>>
>> public <E> Foo<E> blatz() { /* ... */ }
>>
>> can be called like this:
>>
>> final Foo<String> foo = blatz(); // should work; E's upper bounds will be
>> String here
>>
>> But in this test, E has no upper bounds, so how does it work?
>>
>> Thanks,
>> Laird
>>
> _______________________________________________
> weld-dev mailing list
> weld-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/weld-dev
> 

-- 
Martin Kouba
Senior Software Engineer
Red Hat, Czech Republic



More information about the weld-dev mailing list