#FEATURE by exept
authorClaus Gittinger <cg@exept.de>
Fri, 09 Aug 2019 13:06:59 +0200
changeset 24522 e54af87b30cb
parent 24521 9e0ae8e51e4b
child 24523 758ae1a35ecb
#FEATURE by exept class: InlineObject added: #= #displayOn: #hash #isInlineObject comment/format in: #storeOn:
InlineObject.st
--- a/InlineObject.st	Fri Aug 09 13:03:49 2019 +0200
+++ b/InlineObject.st	Fri Aug 09 13:06:59 2019 +0200
@@ -306,16 +306,103 @@
     ^ self == InlineObject.
 ! !
 
+!InlineObject methodsFor:'comparing'!
+
+= someObject
+    "I conside someObject to be equal, if it has the same slotnames"
+
+    |myInstSize|
+
+    myInstSize := self class instSize.
+    someObject class == self class ifFalse:[
+        someObject class instSize == myInstSize ifFalse:[^ false].
+        someObject isInlineObject ifTrue:[
+            self class allInstVarNames keysAndValuesDo:[:i :nm |
+                |mySlotValue otherSlotValue|
+
+                mySlotValue := self instVarAt:i.
+                otherSlotValue := someObject perform:nm asSymbol ifNotUnderstood:[^ false].
+                mySlotValue = otherSlotValue ifFalse:[^ false].
+            ].
+            ^ true
+        ].
+        ^ false
+    ].
+    1 to:myInstSize do:[:i |
+        ((self instVarAt:i) = (someObject instVarAt:i)) ifFalse:[^ false].  
+    ].
+    ^ true.
+
+   "
+    |o1 o2 o3|
+
+    o1 := #{ foo: 1 . bar: 2 }.
+    o2 := #{ foo: 1 . bar: 2 }.
+    self assert:(o1 = o2).       
+    self assert:(o1 == o2) not.    
+    o3 := #{ bar: 2 . foo: 1 }.
+    self assert:(o1 = o3).       
+    self assert:(o2 = o3).       
+   "
+!
+
+hash
+    "I redefine =; so I also have to redefine hash"
+
+    |h myInstSize|
+
+    myInstSize := self class instSize.
+    h := 0.
+    1 to:myInstSize do:[:i |
+        h := h bitXor:((self instVarAt:i) hash) 
+    ].
+    ^ h.
+
+   "
+    |o1 o2 o3|
+
+    o1 := #{ foo: 1 . bar: 2 }.
+    o2 := #{ foo: 1 . bar: 2 }.
+    self assert:(o1 = o2).       
+    self assert:(o1 hash = o2 hash).       
+    self assert:(o1 == o2) not.    
+    o3 := #{ bar: 2 . foo: 1 }.
+    self assert:(o1 = o3).       
+    self assert:(o1 hash = o3 hash).       
+   "
+!
+
+isInlineObject
+    ^ true
+! !
+
 !InlineObject methodsFor:'printing & storing'!
 
+displayOn:aStream
+    aStream nextPutAll:self classNameWithArticle.
+    aStream nextPutAll:' '.
+    self storeOn:aStream.
+
+   "
+    #{ foo: 1 . bar: 2 } displayString
+   "
+!
+
 storeOn:aStream
     aStream nextPutAll:'#{'.
     self class allInstVarNames keysAndValuesDo:[:i :nm |
+        i ~~ 1 ifTrue:[
+            aStream nextPutAll:' . '.
+        ].
         aStream nextPutAll:nm; nextPutAll:':'.
         (self instVarAt:i) storeOn:aStream.
-        aStream nextPutAll:'. '.
     ].
     aStream nextPutAll:'}'.
+
+   "
+    #{ foo: 1 . bar: 2 } storeString                    
+    Object readFrom:( #{ foo: 1 . bar: 2 } storeString)   
+   "
 ! !
 
 !InlineObject::InlineObjectPrototype class methodsFor:'documentation'!