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

comment on "incorrect" comment.
(Added zkl)
(comment on "incorrect" comment.)
Line 1,286:
 
{{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: 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