
[Mike Thompson]
Of course, this obvious solution has a problem: a programmer can't easily sub-class Int. They must choose one a sub-class from Short or Long (unless they want to provide a full implementation themselves) and that's probably not a decision they want to make.
[Guido van Rossum]
(It has been pointed out that subclassing int isn't very useful, so maybe this is a moot point. Does anybody have a real use case?)
I don't. It just rubs my intuition up the wrong way for it not to be subclassable, but that'll be because I'm not a fully reformed OO Bigot ;-) [Mike Thompson]
Perhaps, because of this, the GOF "Bridge Pattern" might be suitable here. (This pattern can be framed as: "adapt multiple implementations to an interface using delegation" -- which, after all, is pretty much what the vtable in previous solution gives you.)
If the existence of Short and Long is an implementation detail best hidden from the python programmer, then a Bridge Pattern implementation has a lot going for it.
[Guido van Rossum]
Hmm... I'm not very familiar with the Bridge pattern (and my GoF book is on one of 65 boxes still in the garage waiting until we move into a larger house :-). Can you give a little more detail about how this would be done?
To me, Bridge Pattern is most useful when either: 1. There is a need to sub-class the abstraction (Int) other than for implementational purposes. 2. The implementation of the abstraction (Int) needs to change at runtime. So, given you doubt the need for 1. and that 2. was never needed because of Int-immutability, I should withdraw my suggestion in favour of the more simple abstract-Int-base-with-Long-And-Short-Iimplementation-in-sub-classes approach previously mentioned. BTW, a summary of most patterns can be found on-line at http://patterndigest.com/ [Guido van Rossum]
Is it something that a user wishing to subclass Int should do, or something that the Int class (or its subclasses) should provide?
Its something that the writer of the Int class would use iff they wanted to ensure that programmers were oblivious to the two or three possible implementations of Int AND one of the two conditions I mentioned above (needed subclassability or runtime-change in implementation) held true. [Mike Thompson]
Use of the Bridge pattern might even allow for three different implementations: PreDefined, Short, Long ... where PreDefined is one of the numbers between 0 and 256 which I'm given to understand are pre-allocated by the runtime environment.
[Guido van Rossum]
I don't think so -- the beauty is that (most of) the implementation doesn't know that certain int values have eternal life; their representation is exactly the same as that of regular ints.
That beauty could remain. At the risk of lapsing completely into pattern vocabulary ... an AbstractFactory would create Ints and bind them to a subsequently hidden implementation. When the int involved was tiny (-1 to 256?) this AbstractFactory would use one of the pre-allocated, shared (see FlyWeight) implementations. Outside of this range distinct Short, Long or DamnHuge implementations would be created. -- Mike