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

From Rosetta Code
Content added Content deleted
(if that is monkeypatching, Smalltalk (afaik) can't. right?)
 
(Try to help understanding of the task description)
Line 8: Line 8:


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. --[[User:ShinTakezou|ShinTakezou]] 13:26, 19 May 2009 (UTC)
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. --[[User:ShinTakezou|ShinTakezou]] 13:26, 19 May 2009 (UTC)

: The task description is clear enough. Add the new things to an instance of the class without modifying the class (i.e., other instances of the class must be unaffected). What would be a more exciting way to do it would be to dynamically create a subclass of the class with the required modifications and then change the class of the instance ''after creation'' to the subclass. For one thing, that's a sequence of operations that shows that a simple type algebra isn't sufficient to understand the object system… –[[User:Dkf|Donal Fellows]] 01:32, 5 January 2010 (UTC)

Revision as of 01:32, 5 January 2010

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)

The task description is clear enough. Add the new things to an instance of the class without modifying the class (i.e., other instances of the class must be unaffected). What would be a more exciting way to do it would be to dynamically create a subclass of the class with the required modifications and then change the class of the instance after creation to the subclass. For one thing, that's a sequence of operations that shows that a simple type algebra isn't sufficient to understand the object system… –Donal Fellows 01:32, 5 January 2010 (UTC)