4 ms·
Wow that takes me back. Back to a conference room where we were talking about Integers in Java. If you made it a class, an Integer could carry along all this ot
by ChuckMcM 12y ago
Wow that takes me back. Back to a conference room where we were talking about Integers in Java. If you made it a class, an Integer could carry along all this other stuff about how big it was, what the operators were, etc. But generating code for it was painful because your code had to do all of these checks when 99% of the time you probably just wanted the native integer implementation of the CPU. And Boolean's were they their own type or just a 1 bit Integer? And did that make an enum {foo, bar, baz, bletch, blech, barf, bingo} just a 3 bit integer?
Integers as types can compile quickly, but then you need multiple types to handle the multiple cases. Essentially you have pre-decoded the size by making into a type.
At one point you had class Number, subclasses Real, Cardinal, and Complex, and within those a constructor which defined their precision. But I think everyone agreed it wasn't going to replace Fortran.
The scripting languages get pretty close to making this a non-visible thing, at the cost of some execution speed. Swift took it to an extreme, which I understand, but I probably wouldn't have gone there myself. The old char, short, long types seem so quaint now.
- viraptor 12y agoJava's initial approach/implementation is just one way to do that. If you actually use objects to represent your integers, then of course it will have to do extra checks. But Java is not the best example here, because with a better language you can drop all those checks at the compile time and get the "native integer implementation" in the code where it's used. Even C can do a very basic compile-time generics since C11. There's no runtime overhead at all in that case. (http://www.robertgamble.net/2012/01/c11-generic-selections.html http://www.robertgamble.net/2012/01/c11-generic-selections.h...)
- barrkel 12y agoCompile-time polymorphism has no need to incur the kinds of costs you're hinting at here. Consider e.g. Ada attributes, range types, 'First, 'Last etc.: these things are all resolvable at compile time.
- pjmlp 12y agoJust because they look like objects to the programmer they don't need to be at implementation level, like the Pascal family of languages.
- xamuel 12y agoThis is only true until the programmer actually uses them like objects. If the programmer, say, sticks the IntObj into a LinkedList<IntObj>, suddenly that integer is going to need some sort of additional overhead associated with it.
- pjmlp 12y agoWhy? It is a matter of how a specific language is implemented, if tagged types are used, how generic code is handled and so on.
- xamuel 12y agoBecause adding/removing an object in a linked list requires some sort of connection between the object and the list (e.g. a "next" field). Regardless whether or not that's abstracted away by the language --- no language can perform magic. (To pre-empt a possible misunderstanding: I'm talking about adding the integer itself to the list, not a copy of a snapshot of its value.)
- sukilot 12y agoWhat? Are you familiar with pointers? If you want a List<*int>, you can have that. "Next" is part of the list cell, not the contained object.
- xamuel 12y agoYes, I'm a bit familiar with pointers ;) You can store the "next" in a list cell if you want, but there still has to be some way to figure out, given just the object, which cell it corresponds to. Well, you could traverse the whole list to find it, but I sure hope you understand why that's a bad idea. Sure, compute a hash---but that computation is overhead. Why do you think Java distinguishes Integer from int? If it were possible to have integers that walked, talked, and quacked like an object, but without any overhead, then we wouldn't use integers. We'd use those things instead.