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

From Rosetta Code
Content added Content deleted
No edit summary
Line 34: Line 34:
class was created. However, for most practical purposes one can
class was created. However, for most practical purposes one can
obtain desired effect in different way. Namely, except for a few
obtain desired effect in different way. Namely, except for a few
low-lever routines slots in Pop11 are accessed via getter and
low-level routines slots in Pop11 are accessed via getter and
setter methods. Getters and setters are like ordinary methods,
setter methods. Getters and setters are like ordinary methods,
but are automatically defined and "know" low level details of
but are automatically defined and "know" low level details of

Revision as of 23:23, 27 May 2008

Task
Add a variable to a class instance at runtime
You are encouraged to solve this task according to the task description, using any language you may know.

This demonstrates how to dynamically add variables to a class instance at runtime. This is useful when the methods/variables are based on a data file that isn't available until runtime. Hal Fulton gives an example of creating an OO CSV parser at An Exercise in Metaprogramming with Ruby. This is referred to as "monkeypatching" by Pythonistas and some others. It's possible in most dynamic OO languages such as Python, Ruby, and Smalltalk.

JavaScript

e = {}       // generic object
e.foo = 1

Perl

Works with: Perl version 5.x
package Empty;

# Constructor. Object is hash.
sub new { return bless {}, shift; }

package main;

# Object.
my $o = Empty->new;

# Set runtime variable (key => value).
$o->{'foo'} = 1;

PHP

class E {};

$e=new E();

$e->foo=1;

Pop11

In Pop11 instance variables (slots) are specified at class creation time and there is no way to add new slot to an instance after its class was created. However, for most practical purposes one can obtain desired effect in different way. Namely, except for a few low-level routines slots in Pop11 are accessed via getter and setter methods. Getters and setters are like ordinary methods, but are automatically defined and "know" low level details of slot access. Pop11 allows dynamic definition of methods, and one can add new methods which work as "getter" and "setter" but do not store data directly in instance. One possibility is to have one instance variable which contains a hastable (this is essentially what Perl solution is doing). Another possibility (used below) is to create na external hashtable. Adding new slots typically make sense if slot name is only known at runtine, so we create method definition (as a list) at runtime and compile it using the 'pop11_compile' procedure.

lib objectclass;

define :class foo;
enddefine;

define define_named_method(method, class);
    lvars method_str = method >< '';
    lvars class_str = class >< '';
    lvars method_hash_str = 'hash_' >< length(class_str) >< '_'
                              >< class_str >< '_' >< length(method_str)
                              >< '_' >< method_str;
    lvars method_hash = consword(method_hash_str);
    pop11_compile([
        lvars ^method_hash = newassoc([]);
        define :method ^method(self : ^class);
            ^method_hash(self);
        enddefine;
        define :method updaterof ^method(val, self : ^class);
            val -> ^method_hash(self);
        enddefine;
    ]);
enddefine;

define_named_method("met1", "foo");
lvars bar = consfoo();
met1(bar) =>  ;;; default value -- false
"baz" -> met1(bar);
met1(bar) =>  ;;; new value

Python

class empty(object):
  pass
e = empty()

If the variable name is known at "compile" time:

e.foo = 1

If the variable name is only at runtime:

setattr(e, name, value)

Ruby

class Empty
end
e = Empty.new
e.instance_variable_set("@foo", 1)
e.instance_eval("class << self; attr_accessor :foo; end")
puts e.foo