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

Content added Content deleted
(Added C# implementation.)
(Added Logtalk example.)
Line 585: Line 585:
NAME: foo
NAME: foo
42</pre>
42</pre>


=={{header|Logtalk}}==
Logtalk supports "hot patching" (aka "monkey patching") using a category, which is a first-class entity and a fine grained units of code reuse that can be (virtally) imported by any number of objects but also used for "complementing" an existing object, adding new functionality or patching existing functionality. Complementing cateogries can be enable or disabled globally or on a per object basis.

The following example uses a prototype for simplicity.

<lang logtalk>
% we start by defining an empty object
:- object(foo).

% ensure that complementing categories are allowed
:- set_logtalk_flag(complements, allow).

:- end_object.

% define a complementing category, adding a new predicate
:- category(bar,
complements(foo)).

:- public(bar/1).
bar(1).
bar(2).
bar(3).

:- end_category.
</lang>

We can test our example by compiling and loading the two entities above and then querying the object:

<lang logtalk>
| ?- foo::bar(X).
X = 1 ;
X = 2 ;
X = 3
true
</lang>


=={{header|Lua}}==
=={{header|Lua}}==