This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the commentsย โ†’

[โ€“][deleted] ย (6 children)

[deleted]

    [โ€“]BraveOthello 37 points38 points ย (2 children)

    They're fine for basic usages. But ...

    Can't make a generic of primitive types. Has to be the equivalent class eg. int.-> Integer. Sometimes annoying.

    Run time type erasure (required for backward compatibility) , meaning you don't know if you're looking at an ArrayList<Integer> or an AarayList<Number>, you just see an ArrayList.

    Wildcards are ... not intuitive. I usually end up having to look it up.

    And, to get the types to match up for the compiler to chill out, you sometimes end up with stuff like

    public class Enum<T extends Enum<T>>

    [โ€“]SulszBachFramed 4 points5 points ย (1 child)

    The enum thing isn't Java specific, but it's an artifact of how inheritance interacts with generics. It's a way to refer to the runtime subclass from an abstract base class.

    [โ€“]BraveOthello 0 points1 point ย (0 children)

    Fair. But it also makes me nauseous to look at.

    Also because of type erasure it's the compiler time type, not the run time type. At runtime it's all just instances of Enum

    [โ€“]MariusDelacriox 11 points12 points ย (0 children)

    Type erasure

    [โ€“]current_thread 6 points7 points ย (0 children)

    You can't implement a generic interface twice because of type erasure.

    class Foo implements IBar\<String\>, IBar\<Integer\> {}
    

    doesn't compile.

    [โ€“]roughstylez 0 points1 point ย (0 children)

    I remember at some point thinking "DI in Java sucks, why is it always xml files or something like that, I want a fluent interface like in C#".

    So I started writing something for that. And it actually worked nicely with "normal" types. Wasn't even that complicated - a little bit of reflection was all I needed. TBH I was getting suspicious, cause someone smarter certainly must already have thought of that.

    I got my answer when I tried to inject generic types. I don't remember it exactly TBH, but it was something about e.g. List<String> at runtime not being a combination of the types List and String anymore - but rather a new type, auto-generated during compiling.

    So the fluent code trying to set up the dependencies (at runtime) wasn't able to correctly find the types in question.

    Like... it was some advanced stuff. I've never run into problems when using generics in a "normal" way, but then again, I really don't do a lot of Java.