https://kotlinlang.org logo
#announcements
Title
# announcements
d

douglarek

03/15/2017, 9:19 AM
@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

alband

03/15/2017, 9:44 AM
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

douglarek

03/15/2017, 9:47 AM
ok, I just wrote that for demo
any resonable codes can be in get method
a

Andreas Sinz

03/15/2017, 9:48 AM
@douglarek what does your code have to do with kotlin?
a

alband

03/15/2017, 9:48 AM
so in your java code you use E and not T... it's what you want ?
d

douglarek

03/15/2017, 9:49 AM
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

alband

03/15/2017, 9:51 AM
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

douglarek

03/15/2017, 9:52 AM
a.get(List.class,"a");
will return
java.lang.Object
just not
java.util.List
when instances A as a raw type
a

Andreas Sinz

03/15/2017, 9:53 AM
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

douglarek

03/15/2017, 9:54 AM
ok
a

alband

03/15/2017, 9:55 AM
+1 with andreas + dont forget java has type erasure
d

douglarek

03/15/2017, 9:59 AM
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

Andreas Sinz

03/15/2017, 10:00 AM
and what exactly is the problem with this code?
d

douglarek

03/15/2017, 10:03 AM
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

Andreas Sinz

03/15/2017, 10:10 AM
are you sure you are using T and E? because Response<String> does not have any impact on your E
d

douglarek

03/15/2017, 10:14 AM
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

Andreas Sinz

03/15/2017, 10:52 AM
@douglarek the code is working fine here
d

douglarek

03/15/2017, 10:53 AM
ok, just do not know why
a

Andreas Sinz

03/15/2017, 9:21 PM
but i guess your code is different than the one you have posted, because T does not have any impact on E