Menu

#1614 Use of floating constants seems impractical, almost impossible

v4.2.0
invalid
nobody
::CONSTANT (1)
none
1
2019-03-23
2019-03-21
No

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.)

Related

Bugs: #1614

Discussion

  • Rick McGuire

    Rick McGuire - 2019-03-21

    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:


    ** [bugs:#1614] Use of floating constants seems impractical, almost
    impossible**

    Status: open
    Group: v4.2.0
    Labels: ::CONSTANT
    Created: Thu Mar 21, 2019 05:17 PM UTC by Graham Norris
    Last Updated: Thu Mar 21, 2019 05:17 PM UTC
    Owner: nobody

    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.)


    Sent from sourceforge.net because you indicated interest in <
    https://sourceforge.net/p/oorexx/bugs/1614/>

    To unsubscribe from further messages, please visit <
    https://sourceforge.net/auth/subscriptions/>

     

    Related

    Bugs: #1614

  • Graham Norris

    Graham Norris - 2019-03-22

    Right, 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.

     
  • Graham Norris

    Graham Norris - 2019-03-22

    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.

     
  • Rony G. Flatscher

    You may want to try:

    o=.object~enhanced(.directory~of(("pi",.methods~pi)))
    say "o~pi:" o~pi
    
    ::constant pi 3.1415
    
     
    • hex

      hex - 2019-03-22

      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
  • Graham Norris

    Graham Norris - 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.)

    /* Test floating constants */
    
    DO x OVER .METHODS~makearray
      SAY "Defined constants:" x
    END
    
    constants = .object~enhanced(.METHODS)
    SAY "constant1:" constants~constant1
    SAY "attribute1:" constants~attribute1
    SAY "attribute2:"  constants~attribute2
    SAY "Attempting to assign to attribute2..."
    constants~attribute2 = "changed"
    SAY "attribute2:"  constants~attribute2
    SAY "Attempting to assign to attribute1..."
    constants~attribute1 = "changed"
    SAY "attribute1:"  constants~attribute1
    
    EXIT
    
    ::CONSTANT constant1 "a constant string of text"
    
    ::ATTRIBUTE attribute1 GET
      RETURN "an attribute string which has no SET method"
    
    ::ATTRIBUTE attribute2 GET
      EXPOSE attribute2
      IF symbol("attribute2") = "LIT" THEN attribute2  = "an attribute string which has a SET method"
      RETURN attribute2
    ::ATTRIBUTE attribute2 SET
      EXPOSE attribute2
      attribute2 = 'ARG'(1)
    

    Results in

    Defined constants: ATTRIBUTE1
    Defined constants: ATTRIBUTE2
    Defined constants: ATTRIBUTE2=
    Defined constants: CONSTANT1
    constant1: a constant string of text
    attribute1: an attribute string which has no SET method
    attribute2: an attribute string which has a SET method
    Attempting to assign to attribute2...
    attribute2: changed
    Attempting to assign to attribute1...
        15 *-* constants~attribute1 = "changed"
    Error 97 running D:\BatCmd\constants.rex line 15:  Object method not found
    Error 97.1:  Object "enhanced Object" does not understand message "ATTRIBUTE1="
    

    The error at the end is expected of course, but using object~enhanced on .methods is always going to include any floating method.

     
    • Rony G. Flatscher

      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.

       
  • Graham Norris

    Graham Norris - 2019-03-22

    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.

     
  • Rony G. Flatscher

    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!

     
  • Graham Norris

    Graham Norris - 2019-03-22

    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.

     
    • hex

      hex - 2019-03-22

      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

       
  • Rony G. Flatscher

    Here is a version of hex example that uses the new ooRexx "do with index idx item it over collection" version:

    do with index name item method over .methods
       select
          when method~isAttribute then say "Defined attribute:" name
          when method~isConstant  then say "Defined constant:" name
          otherwise                    say "Defined method:" name
       end
    end
    
    ::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
    

    The produced output:

    Defined method: TALK
    Defined attribute: SIZE
    Defined constant: PI
    Defined attribute: SIZE=
    Defined constant: TWO
    
     
  • Erich

    Erich - 2019-03-23
    • status: open --> invalid
     

Anonymous
Anonymous

Add attachments
Cancel





MongoDB Logo MongoDB