Smalltalk / Squeak string shallow equality

The following code prints "false":

a := 'aaa'.
b := a deepCopy.
Transcript show: (a == b).

I expect this behavior, and my explanation for this would be that deepCopy returns a new object "b", which is a completely different object than "a", and since the operator == compares by reference, the result is false. It is right?

However, I do not understand why the following code creates "true":

a := 'aaa'.
b := 'aaa'.
Transcript show: (a == b).

Here we made two assignments to two different objects: "a" and "b", and there should not be any relationship between them, except for the fact that they contain the same meaning. But if the operator "==" is compared by reference, and not by value, why is the result of this comparison "true"?

+4
3

, " ?", " ?". , , 'aaa' == 'aaa', . ; , , , !

, , . #deepCopy #shallowCopy, - class basicNew: index . , .

+4

, , true , 'aaa' a b.

, *:

Object subclass: #MyClassA
    instanceVariableNames: ''
    classVariableNames: ''
    poolDictionaries: ''
    category: 'MyApp'


!MyClassA methodsFor: 'testing' stamp: nil prior: nil!
testStrings

    | a b |
    a := 'aaa'
    b := 'aaa'
    ^ a == b
! !
MyClassA testStrings " ==> true"

, :

Object subclass: #MyClassB
    instanceVariableNames: ''
    classVariableNames: ''
    poolDictionaries: ''
    category: 'MyApp'


!MyClassB methodsFor: 'testing' stamp: nil prior: nil!
a

    | a |
    a := 'aaa'
    ^ a
! !
!MyClassB methodsFor: 'testing' stamp: nil prior: nil!
b

    | b |
    b := 'aaa'
    ^ b
! !
!MyClassB methodsFor: 'testing' stamp: nil prior: nil!
testStrings

    ^ self a == self b
! !
MyClassB testStrings " ==> false"

, Squeak ,

, stings.

*: DoIt PrintIt, , Squeak.

+2

, Smalltalk, , :

, . deepCopy , .

, Smalltalk . , , , , , , . , , . , , . , , , , , . true.

0
source

All Articles