Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (568)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  Have we all jumped the gun on Java's implementation of generics?  (Read 6917 times)
0 Members and 1 Guest are viewing this topic.
Offline K.I.L.E.R

Senior Member




Java games rock!


« Posted 2005-06-21 13:35:36 »

I'm sure we all remember my angry post about type erasure?
Well I wanted to know more about how Java implements type erasure, so I found this article:
http://today.java.net/pub/a/today/2003/12/02/explorations.html

It's old, but it effectively mentions that:
Quote
Every type parameter is mapped to the appropriate bound. By this, I mean: the type parameter is erased and replaced with the strongest type the compiler can reasonably assert. Thus, in the case of <T>, T is mapped to Object. In the case of <T extends Rentable>, T is mapped to Rentable, and so on.

So technically, doesn't this mean that generics DON'T cast at runtime, but at compile time?

However, here is a contradiction:
Quote
Casts are inserted wherever necessary, to ensure that the code compiles.

So from the examples and the like, I figure that if Java can figure out what the type is, it will cast it at runtime and at runtime it will act as though there are no casts.
The only time when generics will force casts at runtime is if Javac can't find the type.

So would that mean the best way of implementing generics in your code would be to use interfaces?

Of course this doesn't change the fact you can't map primitive types. Undecided

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #1 - Posted 2005-06-21 14:47:45 »

Generics is just f***ed Tongue.

I've not said anything on it since, but the ACCU conference this year had not one, not two, but three or four (unless there were some others I missed?) sessions explaining in detail how bad they can be ("hopeless" springs to mind), including examples where the statements made in Sun's docs (e.g. things like "you'll never have to worry about X happening, this is not possible by design") were not just mildly inaccurrate but the precise opposite of the truth - and sometimes with supporting commentary for why the implementation choices probably meant this wasn't an accidental bug but forced in by the core design, and hence hard if not impossible to "fix".

IIRC you can get hold of the ACCU slides online at the moment, if you search for them, for most talks...so don't take my word for it (you all know I hate generics anyway Tongue)

malloc will be first against the wall when the revolution comes...
Online princec

JGO Kernel


Medals: 391
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #2 - Posted 2005-06-21 16:10:13 »

I've been using generics very, very heavily for the past 2 months now and I have to say I'm relatively impressed by some of the things that they allow me to express. However the whole concept (and syntax) is quite tricky to grasp and I'm still quite often totally stuck and have to guess my way out of a situation by, say, inserting a ? somewhere or whatever.

The only other real beef I have with generics is that they are not stored as part of the RTTI which means that many nifty optimisations can't be performed.

That, and the fact that primitive types can't be used.

Cas Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline arne

Senior Member




money is the worst drug- we should not let it rule


« Reply #3 - Posted 2005-06-21 17:57:30 »

I think generics makes youre code a bit more understandable and it reduces stupid class cast exception errors, because it just simply wouldn't parse then.

:: JOODE :: Xith3d :: OdeJava ::
Online princec

JGO Kernel


Medals: 391
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #4 - Posted 2005-06-21 19:31:01 »

I'm afraid it's not making it any more understandable really. When it gets slightly hairy it is very, very difficult to figure out what some things actually mean now, what with all these recursive generics definitions etc.

Cas Smiley

Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #5 - Posted 2005-06-24 09:40:26 »

... When it gets slightly hairy it is very, very difficult to figure out what some things actually mean now, what with all these recursive generics definitions etc.

Which was my argument all along, from the very beginning, to all those folks saying "why are you complaining, you don't *have* to use these things?".

It's just that now I've seen how horrible it can (and does) get - and the fact that it even breaks the java compile process! (yes, folks, there's some decidedly odd hacks Sun added to make generics work. Tis very spooky to see in action - check out the ACCU stuff) - it's even worse than I thought Sad.

malloc will be first against the wall when the revolution comes...
Offline K.I.L.E.R

Senior Member




Java games rock!


« Reply #6 - Posted 2005-06-24 10:16:22 »

You guys obviously know more about this than I do.
I think generics are great when used appropriately.

Can someone give me an example of this horrible mess you guys talking about?

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Offline amannis

Junior Newbie





« Reply #7 - Posted 2005-06-24 10:50:38 »

Well, this ain't very pretty anymore:
private static HashMap<String, Class<? extends Entity>> classmap = new HashMap<String, Class<? extends Entity>>();

But these are quite rare cases (at least for me) and I quite like generics overall.
Offline K.I.L.E.R

Senior Member




Java games rock!


« Reply #8 - Posted 2005-06-24 12:38:32 »

Well, this ain't very pretty anymore:
private static HashMap<String, Class<? extends Entity>> classmap = new HashMap<String, Class<? extends Entity>>();

But these are quite rare cases (at least for me) and I quite like generics overall.

That's complex? I've used that.

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Online princec

JGO Kernel


Medals: 391
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #9 - Posted 2005-06-24 12:42:39 »

1  
public abstract class CovalentOwnership<T extends CovalentOwnership<T, C>, C extends Covalent<C> & IBaseDataObject<C>> extends BaseOwnership<CovalentOwnershipType, T, C> {....

Amusingly it's so complicated Eclipse 3.1 RC3 has broken completely.

Cas Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline K.I.L.E.R

Senior Member




Java games rock!


« Reply #10 - Posted 2005-06-24 12:54:36 »

I can read and understand it but I see where the problem is.
Too many generalisations tend to lead you to make wrong assumptions and are very misleading.

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Offline amannis

Junior Newbie





« Reply #11 - Posted 2005-06-24 19:05:01 »

Quote
That's complex? I've used that.
Sure it's not complex but beginning to be ugly to read.
Offline K.I.L.E.R

Senior Member




Java games rock!


« Reply #12 - Posted 2005-06-25 03:32:23 »

If you have to implement that in the first place, I'd say you're doing something wrong.
There are no limits or rules on generics.

I've used them to accomplish much weirder things.
The entire point is to simplify your code, and that's what I'm using them for now.

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Online princec

JGO Kernel


Medals: 391
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #13 - Posted 2005-06-25 10:52:33 »

Simplification was never a goal of generics. It was about catching errors earlier, a feat at which it seems to excel. Unfortunately it is at the expense of readability. I don't really grok generics code at all yet.

Cas Smiley

Offline arne

Senior Member




money is the worst drug- we should not let it rule


« Reply #14 - Posted 2005-06-29 12:55:27 »

Hi
I just want to mention that I stumbled on a pretty stupid inconvenience:
java.util.Properties.
Properties is, as anybody knows, some kind of specialized Hashtable. With Generics we could write Properties extends Hashtable<String,String> and that would be exactly what Properties is: a Hashtable that only contains Strings. But if you look into the API Properties does not extend Hashtable<String,String> no - it extends Hashtable<Object,Object> !!! Ok - we could say why use Properties? we've got Generics - but the reason for using Properties is the ability of loading and saving Properties to/from a stream. And here it comes: If I want to get all keys from the Properties this code should work, shouldn't it? (p are my Properties)
1  
Vector<String> keys =  new Vector<String>(p.keySet());


But I can't, because Properties extends Hashtable<Object,Object> and not Hashtable<String,String>!!
I'll have to do that:
1  
2  
3  
4  
Vector<String> keys = new Vector<String>();
for(Object o : controllers.keySet()) {
   keys.add((String) o);
}


Arrrghhh!!!

:: JOODE :: Xith3d :: OdeJava ::
Offline Heri

Innocent Bystander




Wave a dead chicken over it


« Reply #15 - Posted 2005-06-29 15:01:59 »

With Generics we could write Properties extends Hashtable<String,String> and that would be exactly what Properties is: a Hashtable that only contains Strings.
Not exactly. In 1.4 it was possible to store objects as properties (by calling the inherited Hashtable accessors), so I presume the reason for <Object,Object> was backward compatibility.

But you should be able to cast the whole Set
1  
Vector<String> keys = new Vector<String>((Set<String>)p.keySet());


This causes (correctly) an unchecked cast warning which you can hide by annotating the method with @SuppressWarnings("unchecked"), at least in Eclipse.

I usually like generics very much Tongue (ok, not in this case *g*, since I think 99.99% of all usages <1.5 were string properties)
Offline Raghar

Junior Member




Ue ni taete 'ru hitomi ni kono mi wa dou utsuru


« Reply #16 - Posted 2005-06-29 18:58:59 »

Who cares about generics? They are unneeded.  I actually never wrote anything with generics.

Java.util.Properties should be able to work even with 64 bits characters so Object is actually upward compatibile solution.
Offline arne

Senior Member




money is the worst drug- we should not let it rule


« Reply #17 - Posted 2005-06-29 20:17:37 »

Who cares about generics? They are unneeded.  I actually never wrote anything with generics.

Java.util.Properties should be able to work even with 64 bits characters so Object is actually upward compatibile solution.
But all the special functions of Properties work only for Strings. For everything else you could use Hashtable. I think a good solution would be to have
1  
Properties<K,V> extends Hashtable<K,V>
There wouldn't be then any propblem with downwards compability and I could use Properties<String,String> Wink

:: JOODE :: Xith3d :: OdeJava ::
Online princec

JGO Kernel


Medals: 391
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #18 - Posted 2005-06-29 23:54:42 »

I care about generics, because they have so far trapped at compile time no less than 10 bugs in our code that would only manifest themselves in irritating sneaky rare ways. And I'm getting kinda used to not having to write casts in code now... just need to figure out what arcane combo of ?'s and <>s and extends and supers I need at the top of the class def Wink

Cas Smiley

Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #19 - Posted 2005-06-30 08:24:26 »

I care about generics, because they have so far trapped at compile time no less than 10 bugs in our code

I think we would all like to know what those ten were - it would be a lot better than the unsubstantiated opinions and toy examples that are normally quoted both for and against generics.

malloc will be first against the wall when the revolution comes...
Offline K.I.L.E.R

Senior Member




Java games rock!


« Reply #20 - Posted 2005-06-30 08:50:38 »

Typically when you use advanced polymorphism techniques is when errors come up.
Generics can allow you to do fancy things to avoid these errors.

I honestly want to see performance gains.
Casting from Object to "Type" is very slow.

That article states that most of the time there is no casting, at compile time Object is replaced by "Type" and if "Type" cannot be found, it is then casted to "Type".
Can anyone confirm if this is the case?

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Online princec

JGO Kernel


Medals: 391
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #21 - Posted 2005-06-30 09:04:53 »

Now blah, you really don't want to know what those errors were, we've got 0.5mb of source code and you're suprised that generifying it didn't catch a bunch of class cast exceptions ahead of time?

Cas Smiley

Offline K.I.L.E.R

Senior Member




Java games rock!


« Reply #22 - Posted 2005-06-30 09:49:56 »

I've gone through the Java bytecode and it inded does treat <E> as Object.
There are no compile time replacements.

Oh well.

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #23 - Posted 2005-06-30 11:07:28 »

Now blah, you really don't want to know what those errors were, we've got 0.5mb of source code and you're suprised that generifying it didn't catch a bunch of class cast exceptions ahead of time?

Cas Smiley

Wink well, I thought you could perhaps describe an example or two in plain text, perhaps quote some small sample code fragments involved?

malloc will be first against the wall when the revolution comes...
Online princec

JGO Kernel


Medals: 391
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #24 - Posted 2005-06-30 12:42:27 »

Haa, haven't got the time to go fiddling around looking for this stuff Smiley The errors were all in my minions' code, anyway  Cool

Cas Smiley

Offline Mark Thornton

Senior Member





« Reply #25 - Posted 2005-06-30 14:39:20 »

Wink well, I thought you could perhaps describe an example or two in plain text, perhaps quote some small sample code fragments involved?

One example that I have seen a few times is with the Collection api, where you want to add the content of one collection to another collection, but mistakenly type just add instead of addAll.
Offline Jeff

JGO Coder




Got any cats?


« Reply #26 - Posted 2005-07-19 05:48:24 »

I guess i agree with the sumamtion that generics add run-time checking at the cost of readability.

To me its a bad trade.  There are a few mistakes of the type that Mark mentions that you cna make if not careful that wont be caught at run time.  (ANother is calling map.remove(...) on the value rather then the key) but if your awake and watching what you are doing they are pretty easy to catch and fix and I contend that ocne you've done them once you never will again.

FOr the rest of the errors you can make, run-time type checks catch then quick enough for my tsates. 

Generics might make sense in some absolutely time-ciritcal places in code if they eliminated those run-time checks, but I don't think they can as they are pure syntactic sugar.  There were no VM changes made to support them.

Edit: And I should add that in general the VM is pretty savvy about minizing the impact of the run-time type checking.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Jeff

JGO Coder




Got any cats?


« Reply #27 - Posted 2005-07-19 05:50:25 »

To go slightly off topic...

Autroboxing though IMHO is just evil and a mistake and was added  in a fervor of C# me-too-ism.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline zero

Junior Member





« Reply #28 - Posted 2005-07-19 11:06:16 »

Note: I have no read the complete discussion, so please apologize if I will reapeat something previously mentioned.


Hm, I'm perferctly happy with the new features, which become avaible in the tiger release. This may be because I've worked with C# 2.0 since the .NET Framework 2 Beta was availbale and don't want wot miss anything now in programming in Java.

At first I like the new enumeration type. Although IMHO it wasn't really needed, it is saves a little bit code using the BitVector class and it offers great flexibility compared to other languages.


Auto-boxing is the ony feature I'm watching with a good and a bad eye. The implementation shouldn't be compared to  the one of C# since the C# compiler only performs (forward) auto-boxing. In contrast to that the Java version can do auto-unboxing (back from the reference type to a value type).
Although in most cases the amount of code reduces, there are some issues I don't like because the lack of constancy, e.g.

1  
2  
3  
4  
5  
Integer i1 = 100, i2 = 100; // same as Integer.valueOf(100), i2 = Integer.valueOf(100);
Integer j1 = 100, j2 = 200; // same as Integer.valueOf(200), j2 = Integer.valueOf(200);

System.out.println(i1==i2); // true
System.out.println(j1==j2); //false


Of course the comparison is done on the reference and not the values, but i0,i1 share the same object while j1 and j2 don't. That is because the wrapper classes only have a cache for often used numbers between [-128 /or 0.0,127].


As it comes for generics, I won't take my hands on Java without them. I'm very happy with the implementation, especially that value types cannot be used with them. In C# it is o.k., because you can define own value types. However In Java, you cannot due to generality of reference objects, which I really prefer. (A simple language can be used and optimized more easily).
The reason I don't want to miss generics is not because it reduces potentially number of ClassCastExceptions thrown, I love them because they enable Meta-Programming. See the algebra packe in the code I have uploaded (for Jeff) in another thread, although the code isn't a fast as other, the matrix and vector functionality has to be programmed ones for all types, for which a field is defined. (In this sample case implemented fields are float, double and BigDecimal).


 Cheesy Go Java Go  Cheesy
 
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #29 - Posted 2005-07-19 18:38:45 »

There are a few mistakes of the type that Mark mentions that you cna make if not careful that wont be caught at run time.  (ANother is calling map.remove(...) on the value rather then the key)

Argh! I've done that Sad. Twice, in fact Angry. First time it took several days to fix, second time several hours. Since then, as you say, I've *never* forgotten. Even seing the method name, my first throught is "key not value" Grin.

IMHO this would be a fantastic thing to get rid of. It's worth, oh, about 1 programmer day per year per project. Um. Generics costs, what, tens?, hundreds? of programmer days per year per project. Not a good trade (as Jeff says).

Jeff - I must be ill, I completely and utterly agree with you in all ways Tongue.

malloc will be first against the wall when the revolution comes...
Pages: [1] 2
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (40 views)
2014-09-24 16:13:29

Pippogeek (31 views)
2014-09-24 16:12:22

Pippogeek (21 views)
2014-09-24 16:12:06

Grunnt (47 views)
2014-09-23 14:38:19

radar3301 (29 views)
2014-09-21 23:33:17

BurntPizza (65 views)
2014-09-21 02:42:18

BurntPizza (37 views)
2014-09-21 01:30:30

moogie (44 views)
2014-09-21 00:26:15

UprightPath (53 views)
2014-09-20 20:14:06

BurntPizza (55 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!