#FEATURE by exept
authorClaus Gittinger <cg@exept.de>
Thu, 11 Jul 2019 20:13:13 +0200
changeset 24419 d3a56dbbf696
parent 24418 24eb2af1ca29
child 24420 43e823c36ccd
#FEATURE by exept class: OrderedDictionary class added: #newFromObject:
OrderedDictionary.st
--- a/OrderedDictionary.st	Wed Jul 10 18:39:27 2019 +0200
+++ b/OrderedDictionary.st	Thu Jul 11 20:13:13 2019 +0200
@@ -1,3 +1,5 @@
+"{ Encoding: utf8 }"
+
 "
  COPYRIGHT.
  This is a Manchester Goodie protected by copyright.
@@ -147,6 +149,28 @@
 
 new:anInteger
     ^ (super new:anInteger) initializeOrder:anInteger
+!
+
+newFromObject:anObject
+    "return a new instance with slots named after anObject's instance variables."
+
+    |allNames newInst|
+
+    allNames := anObject class allInstVarNames.
+
+    newInst := self new:(allNames size + anObject basicSize).
+
+    allNames doWithIndex:[:nm :idx |
+        newInst at:nm put:(anObject instVarAt:idx).
+    ].
+    1 to:anObject basicSize do:[:idx |
+        newInst at:idx put:(anObject basicAt:idx).
+    ].
+    ^ newInst
+
+    "
+     OrderedDictionary newFromObject:(100 @ 200)
+    "
 ! !
 
 !OrderedDictionary methodsFor:'accessing'!