OORexx allows you to use ::CONSTANT outside any class to produce a floating constant which is effectively a special case of a floating method.
Short of dynamically creating a class and dynamically adding the constant's method to it, there seems to be no way of using a floating constant. I can list its name using .METHODS and I can supposedly get its source, but that shows up as an empty string. What I can't do is get its value, which appears to be because you can't execute a method (and therefore a constant method) without creating a class to do so.
Is there actually a way of using a floating constant without creating a class to use it? (And if not, what's the point of allowing it? A ::ROUTINE which always returns the same value would then be a more useful implementation of ::CONSTANT. as you can call it.)
Anonymous
A floating ::constant , like any other floating method, it really only
useful if used to dynamically create a class or by setting as a method on a
directory object. This is not a big.
On Thu, Mar 21, 2019 at 1:17 PM Graham Norris norrisg@users.sourceforge.net
wrote:
Related
Bugs:
#1614Right, that just confirms what I thought.
One simple way to improve floating constants woul be to return the value, as-is, as the method's source. Then you could use x = value(.METHODS~constantname~source[1]) on a floating constant. I can't see any way this would introduce incompatibility with anything else as currently no source lines are returned for a constant method.
On second thoughts, that would need to be
interpret "x =" .METHODS~constantname~source[1]
but still much simpler to use than manufacturing a class just to get the value.
You may want to try:
Or this variant, oorexx 5 needed
constant = .object~enhanced(.methods)
say constant~pi
say constant~two
say
do x over .methods~makearray
say "Defined constants:" x
end
exit
::constant pi '3,14'
::constant two 2
Last edit: hex 2019-03-22
Both Rony's and hex's methods work - particularly like the simplicity of hex's - but both do more than work with floating constants, which may or may not be a problem (or even an advantage.)
Results in
The error at the end is expected of course, but using object~enhanced on .methods is always going to include any floating method.
Graham, if you lookup the Method class ("5.1.3. Method Class") in rexxref.pdf, then you will see the new methods "isAttribute", "isConstant", "isPackage", etc. which you could incorporate in your sample.
Rony, you must be looking at v 5 doc. because .method is 5.1.4 in my 4.2 doc. and it doesn't have any of those 3 is... methods.
Graham, indeed, using 5.0 beta. As 5.0 is already stabler than 4.2 and has many useful new features, IMHO it is safe to use 5.0 in production as well!
Until 3 days ago there's not been a Windows build of 5.0 for months, so I've not bothered with it. Maybe now is the time.
with oorexx 5 you can distinguise between the different .methods
do x over .methods~allindexes
if .methods~at(x)~isattribute then
say "Defined attribute:" x
else
if .methods~at(x)~isconstant then
say "Defined constant:" x
else
say "Defined method:" x
end
exit
::constant pi '3,14'
::constant two 2
::method talk
use arg text
say text
::attribute size get
::attribute size set
expose size
use arg value
if datatype(value, "Whole") = .false | value < 0 then
raise syntax 93.906 array ("size", value)
size=value
Here is a version of hex example that uses the new ooRexx "do with index idx item it over collection" version:
The produced output: