Seite 1 von 1
Ex02 Task2.1 Type mismatch error
Verfasst: 12. Nov 2017 16:02
I have a question with regard to Context.get() method. I am wondering how does the Java compiler cast implicitly the return type of the Supplier call value.get() whose return type is of generic type T to Object type. In fact the signature of cache.computeIfAbsent() method is given such as : Object java.util.HashMap.computeIfAbsent(String key, Function<? super String, ? extends Object> mappingFunction)
Since T is an unbound generic type, the compiler should rather raise a type mismatch error which does not occur.
Re: Ex02 Task2.1 Type mismatch error
Verfasst: 13. Nov 2017 09:50
There is no cast necessary.All reference types are a subtype of Object and functions are generally co-variant in their return types and contra-variant in their parameter types.
BTW 1 - In Java Generics only abstract over reference types and Object is the top-most reference type.
BTW 2 -In Scala we have Any and below Any we have AnyRef (~Object) and AnyVal and all primitive types are subtypes of AnyVal; in Scala, a generic can abstract over any type...