• (cs) in reply to OneFactor
    OneFactor:

    Java dealt with the sort() function on collections via: List[T implements Comparable] so that you could make a library function work to sort both on List[String] and List[Date] or any later implementation of Comparable.



    Except that a List[ T implements Comparable ] cannot be inserted into. Oops. :)

    That's why you have to tweak things a bit even with bounded wildcards.
  • (cs) in reply to biziclop
    biziclop:
    OneFactor:

    Java dealt with the sort() function on collections via: List[T implements Comparable] so that you could make a library function work to sort both on List[String] and List[Date] or any later implementation of Comparable.



    Except that a List[ T implements Comparable ] cannot be inserted into. Oops. :)

    That's why you have to tweak things a bit even with bounded wildcards.

    So let's say we have a ArrayList[T implements Comparable] and you do:
    List[String] slist = new ArrayList[String]
    slist.Add(foo);
    slist.Add(foo2);

    Doesn't the compiler make sure foo is String? I feel like I'm missing something and you could enlighten me.

  • mister r (unregistered) in reply to OneFactor

    The advertising of type-safe collections is problely correct: if you use generic code, it will remain type safe. But calling it generic in the first place is problematic, because its not. Its currently just a poor-mans-polymorphy at the most. Type-variables are just half of the cake. The other half is being able to return a lambda function of which the type depends on the type or types of one or more of the given arguments. This actually required some sort of relational mapping. (parametric type classes in Haskell). So thats the full cake, right? Wrong. True generic programming would also mean that any more complex type is built out of simpeler types, so that we can write a type-safe structural equality function for any and every type we will ever create. This is currently possible through recursion, but its not type safe. Again: because the type systems of the main stream frameworks (.Net and Java in specific) are just too weak.

    And for what?, is it really that hard to understand?
    Maybe we should buy them a "What part of types don't you understand?" t-shirt that shows the Hindley/Milner typing rules? as offered here:
    http://www.cafepress.com/skicalc.6225368
    I had a good laugh with them.


  • mister r (unregistered) in reply to hwaite
    hwaite:

    Having worked with generics for a while, I'm not sure that they're possible to implement cleanly.  Forget about backwards compatibility.  What can you do when you know that a collection contains a certain class of items but its generic type is a superclass of the content type?  If you need to pass this collection to a method that accepts only the sub-typed collection, I can't think of any efficient solution that doesn't involve working around generic warnings.  If the compiler rejected these hacks, you'd end up with everything declared with the 'Object' type with the same plethora of casts that generics were meant to eliminate.  I haven't been able to create any body of code consisting of more than ~10000 lines that didn't necessitate the occasional generics warning.



    Not only possible. Quite a number of programming languages actually do get it right: Haskell, Ml, Clean, Dylan ... and what Sun and MS are trying to sell as 'generics' is just a very very very small portion of what it really is. The funny thing is, implementing only a very very small portion like they did, really is more work. In Haskell type variables may appear at many different places:

       Data BTree a = Branch (Tree a) (Tree a) | Leaf a

    Here you define a generic binary tree. This is the sort of thing Sun and MS calls generic, although the rest of the world just calls it a polymorphic datatype. Now, for some real generics. In Haskell (GHC6 or higher) you can say:

      Data BTree a = Branch (Tree a) (Tree a) | Leaf a deriving (Typeable, Data)

    This automatically creates instances of the Class Typeable and Data for the BTree a type. You can now define ans use generic functions such as gmap. gmap takes any type and a function as arguments. It will apply the function to each bottom-node of the type. So imagine you have a BTree Int. You can say "gmap (+1) myTree" and it will update all nodes by one. The interesting thing is that gmap is defined _ONLY ONCE_. You can do the same thing with Java and such using reflection, but this is not type safe.
  • John Hensley (unregistered) in reply to mister r
    Anonymous:
    The funny thing is, implementing only a very very small portion like they did, really is more work.

    No. No it isn't more work, because Sun didn't want to recreate ML or Haskell. It wanted to add features to Java, the language it has already invested in and its customers have invested in. With that goal, it did rather well.

    By the way, did you know that Simon Peyton-Jones now works for Microsoft Research, and helped develop some of the enhancements to C#?


  • KillerRabbit (unregistered) in reply to FriedEggs
    FriedEggs:
    FriedEggs:
    I feel sorry for the minions left to maintain of the colleague's legacy.
    No, really, English is my first language!  I got caught up in figuring out this posting software and sadly, my grammar checking went out the window.


    He must have meant "to dispose of the colleague's legacy" and then backpedaled with a nicer word.

    - Erwin

  • (cs) in reply to Gnome

    Anonymous:
    Generics may sound great but really they are a dirty hack that does not actually provide any type saftey at all.

    List<Integer><INTEGER> myList = new ArrayList<Integer><INTEGER>();
    List list2 = myList;
    list2.add(new String());

    Code compiles, now iterate through your genericized myList and see what happens.

    Yes but the error here is in way related to the use of generics...

    you can get the same error with

    Customer bob = new Customer();
    Object o = bob;
    o="some string";

  • raluth (unregistered) in reply to Bullet

    Can't see anything strictly wrong there (other than re-using a variable (o) to mean multiple different things); you're just reassigning o; completely different scenario.

    Marc

  • MaxExtreme's News For U (unregistered) in reply to UDontWantToKnow

    I'm sure his C++ code looks like this:

    std::vector<void *=""> vector_of_stuff;</void>


    That's the way, baby:

    Come'n to the party:

    class Party : public Object
    {
    private:
        void ** animals;
        const unsigned int size=1000;

        // 1,3,5,7... will be dogs
        // 0,2,4,6... will be cats
        // all right? let's coding

        unsigned int last_dog;
        unsigned int last_cat;

    public:
        Party() : last_dog(1), last_cat(0), animals(new (void*)[size]) { }; // muahahaha i own
        ~Party() { delete [] animals; }

        void AddCat(Cat * kitty) { animals[last_cat++]=static_cast<void *=""><void*>(kitty); }
        void AddCat(Dog* bobby) { animals[last_dog++]=static_cast<void *=""><void*>(bobby); }
        void * GetAnimal(int n) { return n >= 0 && n<size ?="" animals[n="" :="" null="" }=""> < size ? true : false; }
        bool IsCat(unsigned int n) { return n%2==0 ? true : false; }
        bool IsDog(unsigned int n) { return n%2==0 ? false : true; }
    }


    THIS is owning the programming!!

    THIS is a guru-style!!

    lmao </size></void></void>
  • Will (unregistered) in reply to Gnome

    Anonymous:
    Generics may sound great but really they are a dirty hack that does not actually provide any type saftey at all.

    List<INTEGER> myList = new ArrayList<INTEGER>();
    List list2 = myList;
    list2.add(new String());

    Code compiles, now iterate through your genericized myList and see what happens.

    That would be because Java generics are just syntactic sugar.  Generic classes are in fact just classes based on the Object class, with casting and boxing being performed as necessary.

    In C#, however, generic classes are actually generated on the fly, as needed, based on the appropriate class, so no casting is necessary.

  • j0 (unregistered) in reply to John Doe
    John Doe:

    ... That reminds me of something i read about objective-c some years ago. 'Elephants do what elephants do and lemons do what lemons do. Make sure that no elephant, by accident, ends up in your blender for it will surely ruin your breakfast.' ...

    Blended lemons for breakfast?
    WTF?

  • muzzman (unregistered)

    I'm a committed TDWTDF reader! (See article written 7 years from this one)

    Captcha: luptatum - you say luptatum, I say laptutam

Leave a comment on “Generic Generics”

Log In or post as a guest

Replying to comment #:

« Return to Article