5

I'm getting a little annoyed of having to wrap primitive types in wrapper objects to store them in collection data structures (sets, maps, lists, etc.) in languages like Java and Objective C. I'd really like to have, say, a Map data structure that works the same way whether I'm mapping NSNumbers to Strings or integers to doubles or integers to MyBigCustomObject*.

My understanding is that the reason collection data structures in these languages require wrapping in an object is so it can just always assume that the number given is a pointer - I actually can make a NSDictionary with [NSDictionary dictionaryWithObject: MyCustomObject forKey: 1], but it will treat the 1 as a pointer instead of an integer, try to access memory address 1 and segfault. But I don't see any reason why you couldn't make a collection data structure that keeps track of the type of keys and values that wouldn't have this problem. Even a relatively inflexible data structure like a Map of specifically-ints to specifically-pointers-to-objects would be a decently common use case (I certainly could use one, I'm doing a lot of work recently that involves indexing objects by an integer ID). Is there a reason why this isn't a common paradigm in languages that have an Object/primitive type distinction?

2
  • 2
    You ought to check out Delphi. It doesn't have the whole "everything must be an object in order to be useful" philosophy, and you can create generic collections of any type, including primitives or value types, without the need for wrappers or boxing. Aug 23, 2011 at 17:57
  • 2
    (+1) @Mason Wheeler, another Delphi developer who likes & uses O.O.P., but, only when really applies...
    – umlcat
    Aug 26, 2011 at 17:32

4 Answers 4

3

It is, of course, possible. However, it would require duplicating all the code (which can be quite much for an optimized implementation) for each non-reference type, or a way to automatically specialize the implementation for each type.

I assume that nobody considered the first option worthwhile.

The second way adds some complexity to the language and its implementation. Also, in the case of Java, there are generics (which could in principle be used to implemented the second option) but early design decisions (some would say errors) prevent the second option, as generics are compiled to non-generic code that simply uses objects everywhere and casts as necessary.

Note that there is one language, C++, which has a mechanism for the second option (templates). It doesn't treat objects as pointers (reference types) though.

Edit: As FredOverflow points out in the comments, this does exist. Scala exposes the second option to the programmer with a @specialized annotation.

7
  • I believe Scala generates special bytecode for primite types, too, doesn't it? Aug 23, 2011 at 18:31
  • @FredOverflow: I don't know exactly what you mean. As far as I know, scala maps the value types that have a Java/JVM equivalent to those equivalents (i.e. Int addition is compiled to iadd), if that's what you mean.
    – user7043
    Aug 23, 2011 at 18:35
  • I'm talking about Specializing for primitive types. Aug 23, 2011 at 18:38
  • @FredOverflow: Very interesting, thanks for pointing out!
    – user7043
    Aug 23, 2011 at 18:39
  • C# automatically specializes generics for the primitives types (option two), and it is all hidden from the programmer. Sounds exactly like what the OP is looking for.
    – Sjoerd
    Aug 24, 2011 at 1:11
2

This is basically what happens in C++- the language always knows whether something is a pointer or not. Quite simply, there is no fundamental reason that this is required at all- Java and other languages just do it because they enjoy restricting programmer freedom.

3
  • 1
    C# also automatically generates specialized code for primitive types, which enables the compiler to optimize those special cases. See artima.com/intv/genericsP.html
    – Sjoerd
    Aug 24, 2011 at 0:57
  • Given a value like 0xDEADBEEF, how does C++ know whether it's an integer or a pointer?
    – mipadi
    Sep 7, 2011 at 21:24
  • @mipadi it's an int, because numeric literals are ints.
    – Caleth
    May 8, 2018 at 13:37
1

But I don't see any reason why you couldn't make a collection data structure that keeps track of the type of keys and values that wouldn't have this problem.

this is exactly the issue in those languages. Due to the way that Generics are implemented in Java and Objective-C, this information is not available anymore at runtime.

It is conceivable though to implement a data structure whose constructor takes instances of Type in its constructor and using that information to store the data. It sounds like a lot of work though, and the API gets messy. Question to the community: does an implementation of such a data structure already exist?

-1

in Java, this seem to be a common paradigm indeed. One can find plenty 3-rd party libraries for collections like you refer to. Some of these were discussed at SO: What is the most efficient Java Collections library? Searching web for java library high performance collections shows even more, like eg HPPC

  • The reason why such libraries didn't make it into standard Java API (yet?) is most likely that these did not gain popularity sufficient to justify such an inclusion (yet?). Another possible reason is that there seem to be no common agreement (yet?) on how to design standard API for these collections. As Joshua Bloch said, Public APIs, like diamonds, are forever. You have one chance to get it right so give it your best.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.