Jump to content

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

(Added Wren)
Line 1,667:
asks for adding variables to the class, not the instance.
 
CG: that comment refers to the Monkey code below - not the addSlot example.
CG: no, you should read the code carefully: the original class remains unchanged, meaning that future instances of the class ("Point" in the above example) are not affected. Only the one affected instance gets a new class (which is anonymous, as it gets installed into the environment "nil", which means: "nowhere"). Actually, there is no way to get more new instances of that new instance's class because the class of it is only referenced by the changed p. (well, it could be done by using reflection, as with "p class new"). So, the above EXACTLY does what was asked for, whereas the example below is wrongly changing the original class. }}
 
<lang smalltalk>Object subclass: #Monkey
instanceVariableNames: 'aVar'
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.