• The Real WTF (unregistered)

    The Real WTF is that to get an array of type T you have to pass an array of type T to the toArray function.

    Java generic types are just an over-engineered after thought.

  • Simon Clarkstone (unregistered) in reply to The Real WTF

    Some of the Java library classes have methods where the caller must allocate their own array then pass it in to tell said method to write into it, like this was C or something. Perhaps this was intended to allow certain efficient or non-allocating usage of the API. (The Arrays class is full of this.)

  • rosuav (unregistered)

    Sad that getting the keys and values doesn't guarantee that they're in the same order. Languages like Python do guarantee this (as long as the mapping isn't mutated in between).

  • Dude (unregistered)

    Either the tests weren’t checking that, or the tests were assuming the incorrect behavior was correct.

    That's an awfully big assumption you have there.

  • (nodebb)

    Oh come on! We get the idiotic java code but not the mindboggling straight from hell in house programming language??? Please someone submit that instead!!!!

  • (nodebb)

    Ok the method which fills a supplied array should be called fill or copyTo, not toArray. Also the whole requirement has a code smell. Countries are a domain entity. This special built logic creating some custom in memory storage model is very suspect.

  • Anonymous') OR 1=1; DROP TABLE wtf; -- (unregistered) in reply to Dude
    That's an awfully big assumption you have there.

    It's vacuously true. Every test that exists wasn't checking that. Every test that exists assumed incorrect behavior.

  • Somebody Somewhere (unregistered) in reply to The Real WTF

    Leave it to Java to make C++ templates look elegant and simple.

  • Random pedant (unregistered)

    Just one correction: The "entrySet" method returns a collection of entries, not sets.

  • (nodebb) in reply to Anonymous') OR 1=1; DROP TABLE wtf; --

    It's vacuously true. Every test that exists wasn't checking that. Every test that exists assumed incorrect behavior.

    You're straying into a contentious debate in the philosophy of logic and argument here.

    Question: does "All X are Y" imply "There is at least one X"?

    That debate.

    In context: the awfully big assumption is "There were tests."

  • xtal256 (unregistered)

    "Easy Reader Version: TRWTF is Java's toArray method, which- if you pass it an undersized buffer, WILL create an entirely new array, but if you pass it an accurately sized buffer, it won't"

    How is that a WTF? What would you expect it to do with an undersized buffer? Do like C++ and overrun the buffer resulting in security flaws?

  • (nodebb) in reply to Steve_The_Cynic

    Not allowing the elements of the empty set to have a given property is like requiring 1 to be a prime number.

  • (nodebb) in reply to xtal256

    How about throw an exception?

  • (nodebb) in reply to rosuav

    Sad that getting the keys and values doesn't guarantee that they're in the same order.

    All the built-in implementations do guarantee that (because the two collections are actually implemented as views over the collection of entries in the map) but some crazy mofo could do something else. The only saving grace is that they probably didn't as it's a lot more work to do it that way.

  • Assuming (unregistered)

    You are of course assuming the code was EVER tested ;-)

  • Just Me (unregistered) in reply to The Real WTF

    Java Generics were an afterthought, added in Java 5 (internal version 1.5). Since the class file / byte code format already had to be changed with that change, they should have done better than "reify" generic classes. Reification was a compromise to maintain source code backward compatibility.

    It is possible to add a generic type on a method:

    public Z[] toArray<Z>()

    The problem is trying to allocate a new array of generic type Z (in Java you need an instance of Class to create an array, and Type is not quite the same thing). Another problem is that previous versions declared that toArray without any arguments would return an array of type Object.

    They could have worked around this by passing in the class:

    public Z[] toArray<Z>(Class<Z> clazz)

    A smart implementation would return an array of type T, where T is the type of the Collection's elements:

    public T[] toArray()

    Alas, we only have a Type and not a Class, so we can't dynamically create the array!

Leave a comment on “ToArray, then REST a bit”

Log In or post as a guest

Replying to comment #506328:

« Return to Article