Bug: Use of == for arithmetic equality
Dan Ingalls
Dan at SqueakLand.org
Tue Feb 14 01:35:06 UTC 2006
Wolfgang Helbig <helbig at lehre.ba-stuttgart.de> wrote...
>But #== and #= is equivalent in ST-80 as described in [Adele Goldberg,
>David Robson: "Smalltalk-80 The Language", 1989, p 115]:
>
> Objects, that can not change their internal state are called immutable
>objects. This means,
> that, once created, they are not destroyed and then recreated when
>they are needed again.
> Rather, the 256 instances of Character are created at the time the
>system is initialized and
> remain in the system.
> ...
> Besides Characters, the Smalltalk-80 system includes SmallIntegers and
>Symbols as immutable
> objects.
You have to be careful. There are two issues: immutability and "interning" (guaranteeing a unique object for each value). Immutability does not at all guarantee that == will work for arithmetic equality. Squeak's LargeIntegers and Squeak's Floats are designed to be immutable in their protocol, yet == among these is not equivalent to =. It is true that a==b implies a=b, but it is *not* true that "(a==b) not" implies "(a=b) not". To get this second effect requires "interning" wherein the object you get back for a given value is always the same object -- as with characters (there is a table of them), and Symbols (there is a table of them as well). It *happens* that most modern Smalltalks (ie since ST-76 which did *not* have this property) implement SmallIntegers as a pointer with a tag and the value, so any two SmallIntegers of the same value are encoded perforce as the same object, so they are inherently interned.
>In the same book, there are expressions like
>
>[p 139]
>... we want to know how many of the Characters are a or A.
> count _ 0.
> letters do: [:each | each asLowercase == $a
> ifTrue: [count _ count + 1]]
>
>[p 168]
>Thus
> 'a string' asSymbol == 'a string' asSymbol
>answers true.
These are OK exactly for the above reason.
>etc. It might be a bad style to use #== instead
>of #=, but this "bad habit" is certainly not rooted in the usage of
>curly-brace languages alone.
I think we cannot excuse this usage either as merely a bad habit, nor as justifiable because it occurs in other languages. it is simply not Squeak arithmetic. I'm sure your curly-brace languages are equally unhappy with the use of = in place of == .
>Now, my question. Are SmallIntegers, Characters and Symbols in Squeak
>immutable objects in the sense of the above definition, i, e. not
>destroyable? If not, why and when was it changed in Squeak. If they are
>still immutable, why is this planned to be changed?
Yes, they are immutable, but not all Integers are interned. Just evaluate...
(1e10 + 1 - 1) == 1e10
and be convinced.
I know of no plans to change immutability of any of these objects. But there is definitely a plan to stop using == to test arithmetic equality in Squeak ;-).
Thanks for your interest.
- Dan
More information about the Squeak-dev
mailing list
|