Anonymous user
Add a variable to a class instance at runtime: Difference between revisions
Add a variable to a class instance at runtime (view source)
Revision as of 11:36, 19 November 2009
, 14 years agoFixed 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();
</lang>▼
Or by creating a dynamic class
<lang actionscript>package
{
public dynamic class Foo
Line 16 ⟶ 13:
// ...
}
▲}</lang>
<lang actionscript>var foo:Foo = new Foo();▼
foo.bar = "zap";</lang>▼
▲var foo:Foo = new Foo();
▲foo.bar = "zap";
=={{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;
procedure Dynamic is
Line 65 ⟶ 58:
Put_Line (EE.Boo & " with" & Integer'Image (EE.Foo));
end;
end Dynamic;</lang>
Sample output:
<pre>
Line 126 ⟶ 118:
=={{header|Io}}==
=={{header|J}}==
Simple assignment will add variables to classes.
<lang j>
=={{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>
=={{header|Lua}}==
Line 157 ⟶ 149:
=={{header|Perl}}==
{{works with|Perl|5.x}}
=={{header|PHP}}==
=={{header|Pop11}}==
Line 198 ⟶ 190:
it using the 'pop11_compile' procedure.
<lang pop11>lib objectclass;▼
▲lib objectclass;
define :class foo;
Line 226 ⟶ 217:
met1(bar) => ;;; default value -- false
"baz" -> met1(bar);
met1(bar) => ;;; new value</lang>
=={{header|PowerShell}}==
Line 260 ⟶ 250:
=={{header|Python}}==
<lang python>
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>
: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>
=={{header|Slate}}==
Slate objects are prototypes:
<lang slate>define: #Empty -> Cloneable clone.
define: #e -> Empty clone.
e addSlotNamed: #foo valued: 1.</lang>
=={{header|Smalltalk}}==
|