[Seaside] question on tutorial - contact manager
John M McIntosh
johnmci at smalltalkconsulting.com
Sat Feb 20 19:11:45 UTC 2010
On 2010-02-20, at 9:20 AM, Randal L. Schwartz wrote:
> Consider:
>
> a := 'foo'.
> b := 'foo'.
> a = b "true, but done by comparing character by character"
> a == b "false, because they are actually different String objects"
>
> c := #foo.
> d := #foo.
> c = d "true, but mapped into..."
> c == d "also true, they are the same object"
>
> Symbols are often used in code like the above to ensure
> that the comparisons are very quick.
Let me make a few notes about what Randal said:
(a) the example above
a := 'foo'.
b := 'foo'.
a == b
will evaluate to TRUE in a Squeak workspace because the smalltalk parser/compiler sees the fact that
'foo' being a constant can be reused as an optimization and both 'a' and 'b' point to it.
(b) using '==' can be dangerous if you don't understand the implications. For example.
20 factorial == 20 factorial -> false
yet in a workspace
2432902008176640000 == 2432902008176640000 -> true
and to confuse you more then
10 factorial == 10 factorial -> true
(b) is a special case where the resulting integer from '10 factorial' is a SmallInteger, an immediate object, so == considers if
the value of the immediate object is the same, where as the '20 factorial' is a LargePositiveInteger and we are comparing the
Oops pointer for '==' not the value. And of course the 2432902008176640000 in a work space is a constant so (a) applies.
(c) use of a Symbol is more more memory friendly.
foobar gender = #Female
If the foobar object contains 'Female' this could mean if you have 100,000 foobar objects you could have 100,000 strings 'Female'
if the foobar object contains #Female then all 100,000 foobar objects point to just one symbol.
--
===========================================================================
John M. McIntosh <johnmci at smalltalkconsulting.com> Twitter: squeaker68882
Corporate Smalltalk Consulting Ltd. http://www.smalltalkconsulting.com
===========================================================================
More information about the seaside
mailing list