public final class PlayerFinishedAction<
TGame extends TurnBasedGame<
? extends Board,
TPlayer,
? extends Turn<Phase<TPlayer>>,
? extends Phase<TPlayer>>,
TPlayer extends TurnBasedPlayer>
extends DefaultAction<TPlayer, Location> {
You can only imagine the horrors inside a class defined that way. Needless to say, I had to re-evaluate the cost versus benefit that the generification of the library has brought to me.
I’d also like to highlight a previous post of mine about generified bi-directionality, and let it be reminded again that thanks to erasure, this is all in the end just a bunch of casts and overloads. What I mean to say is, make sure that the type-safety generated by the Generics mechanism is something of value and not just a nice touch, as it sometimes can make a simple application messier.
August 13th, 2006 at 1:31 pm
I don’t think generics can be blamed for this library’s mistake of using generics instead of polymorphism.
The visitor design pattern is pretty useful for many cases where you would be tempted to go mad with generics.
I’d expect there to be some generics warnings (mistakes) either in the library itself or in client code.
Using ? doesn’t really gain you much, can you declare named type parameters instead? Even if you don’t use them they may make code easier to read:
public final class PlayerFinishedAction>,
TPhase extends Phase,
TGame extends TurnBasedGame
TPlayer extends TurnBasedPlayer>
extends DefaultAction
What’s this library called, so that I can avoid it? It looks like somebody is just starting out with generics and is making some mistakes.
August 13th, 2006 at 1:35 pm
I’ll just repeat that code with some HTML escaping, see whether it works any better.
Isn’t there some setting to allow previews in WordPress?
public final class PlayerFinishedAction<
TGame extends TurnBasedGame<TBoard,TPlayer,TTurn,TPhase>
TPlayer extends TurnBasedPlayer,
TBoard extends Board,
TTurn extends Turn<Phase<TPlayer>>,
TPhase extends Phase<TPlayer>>
extends DefaultAction<TPlayer, Location> {
August 14th, 2006 at 5:55 pm
Declaring named parameters just breaks your non-generic classes into generics, forcing you to use generics everywhere, even when not needed. The “?” is used because the class PlayerFinishedAction doesn’t care about what board it is played on. If the generics were not erased but compiled as different classes at runtime, noting which board is used would just create an unnecessarily large amount of PlayerFinishedAction.
The library was (and still is) non-generified in the place pointed out in my post - the point I was trying to make is that generics aren’t helping everywhere, and at some points are making things worse to a point of uncompilable code, or generics warnings everywhere.
“What’s this library called, so that I can avoid it?” - I don’t know whether to treat this as a joke or bad manners.. As I said in the post itself, this is a bad practice, and the code didn’t find its way into the library. Obviously polymorphism is the solution.
August 15th, 2006 at 4:30 pm
Replacing the ? with a name doesn’t make PlayerFinishedAction actually care what board it is played on, it just gives a temporary name to it, so that it can be referred to.
Of course, if generics were templates you’d get lots of code, but they’re not, so you won’t. The plan is not to make them into templates, but to add type reification in the same way that C# has. Erasure was written in to allow backward compatibility, and type reification is definitely left in as an option to be added later.
I saw your point about generics not helping everywhere originally. It’s just a hammer problem, if you use the wrong tool you’re going to hit something other than a nail with your hammer.
Generics warnings left over indicate either incomplete generification, or a mistake. Of course, sometimes it’s not your mistake, but a mistake in the generics implementation. For example, due to the restriction on generic arrays, the source code in java.util.ArrayList will compile with warnings. Personally I think Java 5.0 should not have been released until it was compilable without warnings, i.e., the generics code should not have been considered complete.
I did not realise that this was a library you were writing, I thought it was one that you were using, that had recently been generified. It was a half-joke. If I came across a library that tried to use generics as a substitute for polymorphism, which is impossible, I would probably look elsewhere.
August 21st, 2006 at 5:32 pm
Rick,
I agree that generics are a hammer. A lot would use generics to solve all sorts of problems, whereas it should only be used only for type-safety reasons. Even that, however, is not precise - From time to time, one must use the “isAssignableFrom” method and just make the check himself, or accept certain input that Is type-safe and then assert its type. I agree that it wouldn’t show in compile time, and that it might create errors. But the alternative is to uglifly (TM) your code so badly that it’s impossible to maintain and adjust it.
The point of my post was - See what generics can give you, because at certain usages it might take something from you, and that’s usability and readability.
And don’t worry, I wasn’t offended [much].
April 2nd, 2008 at 5:50 am
Generics and type safety all sounds fine and dandy until you go over board and start using the especially “UGLY” notation in Java 6 (well and may be 7 too) to the extreme. For example if you have something like
public class ASimpleDTO>{
…. now we can do all ID, NAME initializations, getters and setters here
}
And i am involved with a project where people are doing things precisely like that. I think if something can be done to improve the notation or may be use Type inference instead of all that extends crap it will be great.
Static Typing things makes it runtime error free (or to a certain degree at least), but over doing it in many cases can obfuscate the actual purpose of the code.
Regads
Vyas, Anirudh
April 2nd, 2008 at 5:56 am
Err,
my code was like:
public class ASimpleDTO,PARENT extends AnotherModel>{
// stuff here
}
all of this above makes me giddy already
Regards
Vyas, Anirudh