Jump to content

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

No edit summary
Line 479:
test.b=1;
test = setfield (test, "c", 3);
</lang>
 
=={{header|ooRexx}}==
ooRexx does not directly expose instance variables to callers. Encapsulated access to instance variables is done via accesser methods for assignment and retrieval. In general, it is not possible to just dynamically add support, but it is possible to construct a class that allows for this to happen.
==Unknown Method Access==
This example traps unknown method calls, then sets or retrieves the values in an encapsulated directory object.
<lang ooRexx>
d = .dynamicvar~new
d~foo = 123
say d~foo
 
d2 = .dynamicvar2~new
d~bar = "Fred"
say d~bar
 
-- a class that allows dynamic variables. Since this is a mixin, this
-- capability can be added to any class using multiple inheritance
::class dynamicvar MIXINCLASS object
::method init
expose variables
variables = .directory~new
 
-- the UNKNOWN method is invoked for all unknown messages. We turn this
-- into either an assignment or a retrieval for the desired item
::method unknown
expose variables
use strict arg messageName, arguments
 
-- assignment messages end with '=', which tells us what to do
if messageName~right(1) == '=' then do
variables[messageName~left(messageName~length - 1)] = arguments[1]
end
else do
return variables[messageName]
end
 
 
-- this class is not a direct subclass of dynamicvar, but mixes in the
-- functionality using multiple inheritance
::class dynamicvar2 inherit dynamicvar
::method init
-- mixin init methods are not automatically invoked, so we must
-- explicitly invoke this
self~init:.dynamicvar
 
</lang>
 
==Dynamic Method Definitions==
An object may be written that can dynamically add methods to itself. This example is similar to the above example, but the UNKNOWN method attaches a getter/setter pair of methods for the name triggering the UNKNOWN call. On all subsequent calls, the attribute methods will get called.
<lang ooRexx>
d = .dynamicvar~new
d~foo = 123
say d~foo
 
-- a class that allows dynamic variables. Since this is a mixin, this
-- capability can be added to any class using multiple inheritance
::class dynamicvar MIXINCLASS object
::method init
expose variables
variables = .directory~new
 
-- the unknown method will get invoked any time an unknown method is
-- used. This UNKNOWN method will add attribute methods for the given
-- name that will be available on all subsequent uses.
::method unknown
expose variables
use strict arg messageName, arguments
 
-- check for an assignment or fetch, and get the proper
-- method name
if messageName~right(1) == '=' then do
variableName = messageName~left(messageName~length - 1)
end
else do
variableName = messageName
end
 
-- define a pair of methods to set and retrieve the instance variable. These are
-- created at the object scope
self~setMethod(variableName, 'expose' variableName'; return' variableName)
self~setMethod(variableName'=', 'expose' variableName'; use strict arg value;' variableName '= value' )
 
-- reinvoke the original message. This will now go to the dynamically added
methods
forward to(self) message(messageName) arguments(arguments)
 
</lang>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.