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

From Rosetta Code
Content added Content deleted
(timestamp)
(→‎What's exactly monkeypatching?: Does it matter? if It's right in its qualified context.)
Line 14: Line 14:


::So, if something else is meant, I strongly suggest modifying the question to leave no question about it. --[[User:dcsobral|Daniel Sobral]] 02:04, 05 Jan 2010 (UTC)
::So, if something else is meant, I strongly suggest modifying the question to leave no question about it. --[[User:dcsobral|Daniel Sobral]] 02:04, 05 Jan 2010 (UTC)

The page title and task description seem to accurately describe what is needed to complete the task. The use of the word "monkeypatching" is qualified by the use of the phrase "by Pythonistas and some others". This may not be how Monkey patching can be applied in other dynamic languages but the task descriptions use of the qualifier seems to me to make it OK? --[[User:Paddy3118|Paddy3118]] 06:52, 5 January 2010 (UTC)

Revision as of 06:52, 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)
The task description is certainly not clear enough if it means what you say it means, Donal. One reason for disagreement is now obvious to me: in Smalltalk, classes are objects like any other. So a "class instance" is just the run-time representation of a class, as opposed to "an instance of a class". But that's actually a minor point. The major point is that it refers to Patch, which on Ruby and Python communities refers to changing a class, not an object. That's so much true that one of the criticism leveled at monkey patching as practiced in the Ruby community is that sometimes different libraries perform incompatible changes to class used by both, and that is very difficult to track.
So, if something else is meant, I strongly suggest modifying the question to leave no question about it. --Daniel Sobral 02:04, 05 Jan 2010 (UTC)

The page title and task description seem to accurately describe what is needed to complete the task. The use of the word "monkeypatching" is qualified by the use of the phrase "by Pythonistas and some others". This may not be how Monkey patching can be applied in other dynamic languages but the task descriptions use of the qualifier seems to me to make it OK? --Paddy3118 06:52, 5 January 2010 (UTC)