What is the difference between = and == in Pharo Smalltalk?

What is the difference between =and ==in Pharo Smalltalk? What did they call one isEqualand the other?

= ~=    equality / inequality (deep)
== ~~   equality / inequality (shallow)
+1
source share
3 answers

Yes, it ==is an identifier, and it uses a primitive for comparison if the pointers point to the same address (i.e. the same object).

=- This is equality, which means that two objects are equal, although they can be two different objects. By default =uses ==, but it can be redefined. Also, when you override =, it is recommended that you also override hash, so hashed collections don't go crazy

+2

, = , . = , : includes:, <=, >=, remove:, keyAtValue:, indexOf:, upTo:, peekFor:, occurrencesOf:, add: ( Set), at: ( at:ifAbsent:, at:ifAbsentPut: ..) .

, = , ,

  • = (, banana = car )
  • , =, hash.

, =.

, (Set, Dictionary, Bag ..), ,

      IF a = b THEN a hash = b hash

, , hash SmallIntegers, ,

      IF a = b THEN a hash == b hash

, , , hash .


=, a = b, , a b a b.

a == b Smalltalkers , a b - , a b a a a - b.

= , , , Smalltalk .

== , , (VM), , . , a b , .

< >

a := 'This String'.
b := 'This' , ' ', 'String'.
a == b "false".
a = b "true"

f := 2 / 3.
g := 2 / 3.
f = g "true".
f == g "false"

SmallInteger, =, ==, .

n := 3 + 4.
m := 2 + 5.
n = m "true".
n == m "true".

Symbol

s := #symbol.
t := 'symbol' asSymbol.
s = t "true".
s == t "true!"

, Symbol , .

+1

TL; DR: =: "", ==: " , "

β†’ # =

= anObject 
"Answer whether the receiver and the argument represent the same 
object. If = is redefined in any subclass, consider also redefining the 
message hash."

^self == anObject 

ProtoObject β†’ # ==

== anObject    ". :    ( ). ==    ! Essential. . .   . whatIsAPrimitive."

<primitive: 110>
self primitiveFailed

, =, ==.

FWIW ProtoObject subclass: #Object

# =

'= aBag    " ,    () - " " .    () .    (c) .

(aBag isKindOf: Bag) ifFalse: [^false].
self size = aBag size ifFalse: [^false].
contents associationsDo: [:assoc|
    (aBag occurrencesOf: assoc key) = assoc value
        ifFalse: [^false]].
^true`

Dictionary - ( Bas "" ).

` β†’ # = aDictionary    " ,    () - " ".    (b) .    (c) () .   . 16760 "

self == aDictionary ifTrue: [^true].
self species == aDictionary species ifFalse: [^false].
self size = aDictionary size ifFalse: [^false].
self associationsDo: [:assoc|
    (aDictionary at: assoc key ifAbsent: [^false]) = assoc value
        ifFalse: [^false]].
^true`

, hash.

` β†’ #    " - , ,     - - ;     - -"

| hash |

hash := self species hash.
self size <= 10 ifTrue:
    [self do: [:elem | hash := hash bitXor: elem hash]].
^hash bitXor: self size hash

` <= 10, , .

. :

Value>> #hash implementers

.

Spotter, hash #im = #im , .

enter image description here

0
source

Source: https://habr.com/ru/post/1667286/


All Articles