Jump to content

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

m
Fixed lang tags.
m (→‎{{header|J}}: Add lang tags)
m (Fixed lang tags.)
Line 4:
=={{header|ActionScript}}==
In ActionScript this can be done using an Object object
<lang actionscript>var object:Object = new Object();
var object:Object.foo = new Object()"bar";</lang>
object.foo = "bar";
</lang>
Or by creating a dynamic class
<lang actionscript>package
package
{
public dynamic class Foo
Line 16 ⟶ 13:
// ...
}
}</lang>
}
<lang actionscript>var foo:Foo = new Foo();
</lang>
foo.bar = "zap";</lang>
<lang actionscript>
var foo:Foo = new Foo();
foo.bar = "zap";
</lang>
 
=={{header|Ada}}==
Ada is not a dynamically typed language. Yet it supports mix-in inheritance, run-time inheritance and interfaces. These three allow us to achieve the desired effect, however questionably useful it could be. The example declares an interface of the class (Class). Then a concrete type is created (Base). The object E is an instance of Base. Later, at the run time, a new type Monkey_Patch is created such that it refers to E and implements the class interface per delegation to E. Monkey_Patch has a new integer member Foo and EE is an instance of Monkey_Path. For the user EE appears as E with Foo.
<lang ada>with Ada.Text_IO; use Ada.Text_IO;
with Ada.Text_IO; use Ada.Text_IO;
 
procedure Dynamic is
Line 65 ⟶ 58:
Put_Line (EE.Boo & " with" & Integer'Image (EE.Foo));
end;
end Dynamic;</lang>
</lang>
Sample output:
<pre>
Line 126 ⟶ 118:
 
=={{header|Io}}==
<lang io>Empty := Object clone
 
e := Empty clone
e foo := 1</lang>
 
=={{header|J}}==
Simple assignment will add variables to classes.
 
<lang j> C=:<'exampleclass'
V__C=: 0
OBJ=:conew 'exampleclass'
V__OBJ
0
W__C
|value error
W__C=: 0
W__OBJ
0</lang>
 
=={{header|JavaScript}}==
This kind of thing is fundamental to JavaScript, as it's a prototype-based language rather than a class-based one.
<lang javascript> e = {} // generic object
e.foo = 1
e["bar"] = 2 // name specified at runtime</lang>
 
=={{header|Lua}}==
Line 157 ⟶ 149:
=={{header|Perl}}==
{{works with|Perl|5.x}}
<lang perl>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;</lang>
 
=={{header|PHP}}==
<lang php>class E {};
 
$e=new E();
 
$e->foo=1;
 
$e->{"foo"} = 1; // using a runtime name</lang>
 
=={{header|Pop11}}==
Line 198 ⟶ 190:
it using the 'pop11_compile' procedure.
 
<lang pop11>lib objectclass;
<pre>
lib objectclass;
 
define :class foo;
Line 226 ⟶ 217:
met1(bar) => ;;; default value -- false
"baz" -> met1(bar);
met1(bar) => ;;; new value</lang>
</pre>
 
=={{header|PowerShell}}==
Line 260 ⟶ 250:
=={{header|Python}}==
 
<lang python> class empty(object):
pass
e = empty()</lang>
 
If the variable (attribute) name is known at "compile" time (hard-coded):
Line 275 ⟶ 265:
Because functions are first class objects in Python one can not only add variables to instances. One can add or replace functionality to an instance. Doing so is tricky if one wishes to refer back to other instance attributes since there's no "magic" binding back to "self." One trick is to dynamically define the function to be added, nested within the function that applies the patch like so:
 
<lang python> class empty(object):
def __init__(this):
this.foo = "whatever"
 
def patch_empty(obj):
def fn(self=obj):
print self.foo
obj.print_output = fn
 
e = empty()
patch_empty(e)
e.print_output()
# >>> whatever</lang>
:Note: The name ''self'' is not special; it's merely the pervasive Python convention. In this example I've deliberately used ''this'' in the class definition to underscore this fact. The nested definition could use any name for the "self" object. Because it's nested the value of the object is evaluated at the time that the patch_empty() function is run and thus the function being patched in has a valid reference to the object into which it is being inserted. Other arguments could be passed as necessary. Such techniques are not recommended; however they are possible.
 
=={{header|Ruby}}==
<lang 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</lang>
 
=={{header|Slate}}==
Slate objects are prototypes:
<lang slate>define: #Empty -> Cloneable clone.
define: #Empty -> Cloneable clone.
define: #e -> Empty clone.
e addSlotNamed: #foo valued: 1.</lang>
</lang>
 
=={{header|Smalltalk}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.