Add a variable to a class instance at runtime: Difference between revisions
Content added Content deleted
(→{{header|Smalltalk}}: commented the wrong example (long time passed since and original author did not care)) |
|||
Line 1,661: | Line 1,661: | ||
p z. |
p z. |
||
p z:40. |
p z:40. |
||
p inspect |
p inspect. "shows 3 slots" |
||
"Point class is unaffected:" |
|||
p2 := Point x>20 y:30. |
|||
p2 z:40. -> error; Point does not implement z: |
|||
p2 inspect. "shows 2 slots" |
|||
"but we can create another instance of the enhanced point (even though its an anon class)" |
|||
p3 := p class new. |
|||
p3 x:1 y:2. |
|||
p3 z:4. |
|||
p3 inspect. "shows 3 slots" |
|||
</lang> |
|||
<!-- |
|||
{{incorrect|Smalltalk|It extends the class (adds a new instance var and new method that will exists even in brand new future instances of that class), not only the particular instance. -- The description of the problem must be reworded then, as it |
{{incorrect|Smalltalk|It extends the class (adds a new instance var and new method that will exists even in brand new future instances of that class), not only the particular instance. -- The description of the problem must be reworded then, as it |
||
asks for adding variables to the class, not the instance. |
asks for adding variables to the class, not the instance. |
||
CG: commented the bad example; maybe the original author wants to fix it / comment on it. |
|||
CG: that comment refers to the Monkey code below - not the addSlot example. |
|||
<lang smalltalk>Object subclass: #Monkey |
<lang smalltalk>Object subclass: #Monkey |
||
Line 1,738: | Line 1,748: | ||
x: 10 |
x: 10 |
||
10</pre> |
10</pre> |
||
--> |
|||
=={{header|Swift}}== |
=={{header|Swift}}== |