Generic Lists?

109 views
Skip to first unread message

Yissachar Radcliffe

unread,
Apr 13, 2015, 4:40:50 PM4/13/15
to j2objc-...@googlegroups.com
I have a POJO which contains a List<Foo>. When this is translated by j2objc it loses the generic type safety and I'm forced to cast to Foo when trying to do anything with the objects that I get from the List.

I understand that Objective-C doesn't support generics but isn't j2objc in a position where it could generate a class which subclasses JavaUtilList and overrides methods to be type safe for Foo?

Is there any way to accomplish this or am I stuck casting everything? I am using Swift if that makes any difference.

Tom Ball

unread,
Apr 13, 2015, 5:07:31 PM4/13/15
to j2objc-...@googlegroups.com
You're describing type-erasure -- Java classes don't have generic collections, either. Casts are necessary for native Objective-C to safely use generic lists, but not between translated files because the translator already verified those references are safe.

--
You received this message because you are subscribed to the Google Groups "j2objc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to j2objc-discus...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Keith Stanger

unread,
Apr 15, 2015, 9:37:21 AM4/15/15
to j2objc-...@googlegroups.com
The subclassing idea would generate piles of extra code. Imagine if we generated an extra Objective-C class for every declared generic type!

Can you try using a helper function or macro to hide away all the casts?
Reply all
Reply to author
Forward
0 new messages