<@U47K079R7> ``` class A&lt;T&gt; { public &lt;E...
# announcements
d
@alband
Copy code
class A<T> {
  public <E> E get(Class<E> klass, String key) {
    return (E) (new Object()); // just for demo
  }

  public static void main(String[] args) {
    A a = new A<>();
    a.get(List.class,"a");
  }
a
you re instanciating an Object (not a E) and down casting it to E => it should always result a classcastexception if E is not object
you have "klass" that should be used to instanciate a E
what are you trying to do ?
d
ok, I just wrote that for demo
any resonable codes can be in get method
a
@douglarek what does your code have to do with kotlin?
a
so in your java code you use E and not T... it's what you want ?
d
yeah, I want to use <E> not <T> as generic type in
get
method, although A's generic type is <T>
@Andreas Sinz it is not about kotlin, I think, sorry for that
a
what is the problem then ?
if you want E to be a super type of T you can use
E super T
.... but yeah this has nothing to do here, on kotlinlang slack
d
a.get(List.class,"a");
will return
java.lang.Object
just not
java.util.List
when instances A as a raw type
a
give us a real world example on what you are trying to do. i'm still not sure what you are trying to accomplish
d
ok
a
+1 with andreas + dont forget java has type erasure
d
Copy code
public class Response<T> {
  private Map<String, Object> data = new HashMap<>();

  public <E> E getDataEntryAs(Class<E> type, String key) {

    Object o = data.get(key);
    if (o == null) {
      return null;
    }
    ObjectMapper m = new ObjectMapper();
    return m.convertValue(o, type);
  }
}
a
and what exactly is the problem with this code?
d
Copy code
Response response =  new Response();
    
response.getDataEntryAs(String.class,"");
just do not know why
response.getDataEntryAs(String.class,"");
return
java.lang.Object
if
Copy code
Response<String> response =  new Response<>();
response.getDataEntryAs(String.class,"");
response.getDataEntryAs(String.class,"");
will return
java.lang.String
a
are you sure you are using T and E? because Response<String> does not have any impact on your E
d
I have other methods will use <T>, but for
get
it just use <E>
if response has raw type not generic type, <E> will not work
a
@douglarek the code is working fine here
d
ok, just do not know why
a
but i guess your code is different than the one you have posted, because T does not have any impact on E