Talk:Add a variable to a class instance at runtime

From Rosetta Code
Revision as of 13:26, 19 May 2009 by rosettacode>ShinTakezou (if that is monkeypatching, Smalltalk (afaik) can't. right?)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

What's exactly monkeypatching?

Trying to fix Smalltalk... The text explains clearly what is requested... but from where it came the information Smalltalk can do that monkeypatching? As far as I know, and as far as I know the language, it can do the monkeypatching explained in wikipedia (and so it can Objective-C), i.e.

A monkey patch is a way to extend or modify the runtime code of dynamic languages without altering the original source code.

This explanation fits with what I know Obj-C and Smalltalk can do, and implies something a little bit different from what is required to accomplish the task; an example could be to change the (NS)String class so that it prints everything reversed always, ... even in already written code... i.e. we changed the behaviour of already existing code without altering the original source code, just adding/modifying a "feature" to classes.

So, as far as I know (I am not a Smalltalk guru anyway), Smalltalk can't add instance variables to an instance of a class, but only to a class, and all already created instances of that class will take the new variables (or methods, whichever was added), no matter if they were created before or after patching.

To achieve what requested, one could just create a "special" use-once subclass and then create the instance of it... but this is not so special, we already know that every OO language can, and it seems to me it would not be exactly what the task wants. --ShinTakezou 13:26, 19 May 2009 (UTC)