Jump to content

Add a variable to a class instance at runtime: Difference between revisions

→‎{{header|Smalltalk}}: commented the wrong example (long time passed since and original author did not care)
(→‎{{header|Smalltalk}}: commented the wrong example (long time passed since and original author did not care))
Line 1,661:
p z.
p z:40.
p inspect</lang>. "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
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
Line 1,738 ⟶ 1,748:
x: 10
10</pre>
-->
 
=={{header|Swift}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.