Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

New to ooRexx: Trying to write a class with attributes, but don't understand what I'm seeing

64 views
Skip to first unread message

robhb...@gmail.com

unread,
May 20, 2020, 12:55:46 AM5/20/20
to
I'm used to TSO (mainframe) REXX, but I'm also comfortable writing classes in VBA and VBS. So I figured ooRexx would be a pleasurable snap. Ok, so I have a lot more to learn than I expected.

I see in the documentation that the ::attribute directive ... well, I think it's equivalent to establishing a "property" in VBA. So I write a simple program like this:

opar=.par~new('E')
say opar~typ opar~hdr opar.fvar
opar~hdr="Explanation"
say opar~typ opar~hdr opar.fvar
exit

::class Par
::attribute typ --1 char
::attribute hdr --text
::attribute fvar --boolean
::method init
trace 'I'
arg typ
select
when typ='A' then do; fvar=0; hdr="Alpha"; end
when typ='E' then do; fvar=1; hdr='Explanation'; end
otherwise nop; end

What I expect is that ::attribute directives establsh three attributes of the Par object, and I will be able to get at them from the main program. When I run the program, the trace inside the INIT method shows that typ, hdr and fvar are indeed being assigned values. But the first SAY command displays "TYP HDR FVAR". Then, after the assignment statement, the second SAY displays "TYP Explanation FVAR".

So the ::attribute directive isn't accomplishing what I think the documentation says it should. And I'm suspicious of my reading anyway, because not one of the sample programs that came with ooRexx contain any ::attribute directives. Yet properties (as they're called in VBA) are so fundamental a part of classes that I figure I must be missing something basic.

---

Jumping to conclusions, here: Someone's going to say "You have to use EXPOSE or SELF~. I haven't figured out yet what SELF is for, but it says here EXPOSE makes variables available to methods, whereas what I want is the reverse, to make attributes available to the main program, ie the caller of the class. And anyway the documentation explicitly says that the ::attribute statement creates two methods, like this:

::method "NAME=" /* attribute set method */
expose name /* establish direct access to object variable (attribute) */
use arg name /* retrieve argument and assign it to the object variable */
::method name /* attribute get method */
expose name /* establish direct access to object variable (attribute) */
return name /* return object's current value */

What am I missing, please?

Rick McGuire

unread,
May 20, 2020, 6:24:03 AM5/20/20
to
On Wednesday, May 20, 2020 at 12:55:46 AM UTC-4, robhb...@gmail.com wrote:
> I'm used to TSO (mainframe) REXX, but I'm also comfortable writing classes in VBA and VBS. So I figured ooRexx would be a pleasurable snap. Ok, so I have a lot more to learn than I expected.
>
> I see in the documentation that the ::attribute directive ... well, I think it's equivalent to establishing a "property" in VBA. So I write a simple program like this:
>
> opar=.par~new('E')
> say opar~typ opar~hdr opar.fvar
> opar~hdr="Explanation"
> say opar~typ opar~hdr opar.fvar
> exit
>
> ::class Par
> ::attribute typ --1 char
> ::attribute hdr --text
> ::attribute fvar --boolean
> ::method init
expose type fvar hdr -- expose needed to access the object variables
> trace 'I'
> arg typ
> select
> when typ='A' then do; fvar=0; hdr="Alpha"; end
> when typ='E' then do; fvar=1; hdr='Explanation'; end
> otherwise nop; end
>
> What I expect is that ::attribute directives establsh three attributes of the Par object, and I will be able to get at them from the main program. When I run the program, the trace inside the INIT method shows that typ, hdr and fvar are indeed being assigned values. But the first SAY command displays "TYP HDR FVAR". Then, after the assignment statement, the second SAY displays "TYP Explanation FVAR".
>
> So the ::attribute directive isn't accomplishing what I think the documentation says it should. And I'm suspicious of my reading anyway, because not one of the sample programs that came with ooRexx contain any ::attribute directives. Yet properties (as they're called in VBA) are so fundamental a part of classes that I figure I must be missing something basic.
>
> ---
>
> Jumping to conclusions, here: Someone's going to say "You have to use EXPOSE or SELF~. I haven't figured out yet what SELF is for, but it says here EXPOSE makes variables available to methods, whereas what I want is the reverse, to make attributes available to the main program, ie the caller of the class. And anyway the documentation explicitly says that the ::attribute statement creates two methods, like this:
>
Expose makes the object variable available to the method (including INIT). With out the EXPOSE instruction, your INIT method is just assigning values to local variables that will go away once the method terminates.

robhb...@gmail.com

unread,
May 20, 2020, 5:49:28 PM5/20/20
to
So without EXPOSE, an attribute's not visible to the methods IN THE SAME CLASS? Isn't that a little weird?

In that case what do PUBLIC and PRIVATE mean on an ::attribute? Oh, I guess only whether the attribute is available to the main program. Still weird.

Don't get me wrong, I love REXX - been using it for decades now - and I love oo programming, once I finally got the hang of it. So I expect to love ooRexx...once I get the hang of it :).

Ok, next question: My method now assigns values correctly, once I add the EXPOSE statement. But it doesn't seem to work for a constant:

::class C
::constant typs 'TESOV'
::method init
expose typs
say typs

When I create a new object of type C, it displays "TYPS"; the INIT method apparently can't see the constant. My main program can, though; when I ask it to SAY OBJ~TYPS it displays "TESOV". Same question, I guess; what am I missing? Notice I attempted to use EXPOSE, but that apparently didn't do the job.

--- On Wednesday, May 20, 2020 at 6:24:03 AM UTC-4, Rick McGuire wrote:
> Expose makes the object variable available to the method (including INIT). With out the EXPOSE instruction, your INIT method is just assigning values to local variables that will go away once the method terminates.
>
> --- On Wednesday, May 20, 2020 at 12:55:46 AM UTC-4, robhb...@gmail.com wrote:
> > ...I see in the documentation that the ::attribute directive ... well, I think it's equivalent to establishing a "property" in VBA. So I write a simple program like this:
> >
> > opar=.par~new('E')
> > say opar~typ opar~hdr opar.fvar
> > opar~hdr="Explanation"
> > say opar~typ opar~hdr opar.fvar
> > exit
> >
> > ::class Par
> > ::attribute typ --1 char
> > ::attribute hdr --text
> > ::attribute fvar --boolean
> > ::method init
> > trace 'I'
> > arg typ
> > select
> > when typ='A' then do; fvar=0; hdr="Alpha"; end
> > when typ='E' then do; fvar=1; hdr='Explanation'; end
> > otherwise nop; end
> >
> > ...it says here EXPOSE makes variables available to methods, whereas what I want is the reverse, to make attributes available to the main program, ie the caller of the class. And anyway the documentation explicitly says that the ::attribute statement creates two methods, like this:

Rick McGuire

unread,
May 20, 2020, 6:21:41 PM5/20/20
to
On Wednesday, May 20, 2020 at 5:49:28 PM UTC-4, robhb...@gmail.com wrote:
> So without EXPOSE, an attribute's not visible to the methods IN THE SAME CLASS? Isn't that a little weird?

An attribute is not a variable, it is a shorthand for declaring methods. The attribute is only accessed by sending a message to the object. If you wish, you can use "self~attributeName" in the init method to accomplish the same thing, but using expose is much more efficient.


>
> In that case what do PUBLIC and PRIVATE mean on an ::attribute? Oh, I guess only whether the attribute is available to the main program. Still weird.

PRIVATE means the same as it does with any method declaration. It determines what happens when you send a message to that object from a different scope. Declaring an attribute PRIVATE means it can only be accessed from a method running on the same object instance. This is most useful if you have attributes that you do not want widely available, but are still available to subclasses of the declaring class.

>
> Don't get me wrong, I love REXX - been using it for decades now - and I love oo programming, once I finally got the hang of it. So I expect to love ooRexx...once I get the hang of it :).
>
> Ok, next question: My method now assigns values correctly, once I add the EXPOSE statement. But it doesn't seem to work for a constant:
>
> ::class C
> ::constant typs 'TESOV'
> ::method init
> expose typs
> say typs
>

::CONSTANT is also just a short hand method declaration. It only returns that value when invoked via "~". It does NOT set any instance variables of the object.

> When I create a new object of type C, it displays "TYPS"; the INIT method apparently can't see the constant. My main program can, though; when I ask it to SAY OBJ~TYPS it displays "TESOV". Same question, I guess; what am I missing? Notice I attempted to use EXPOSE, but that apparently didn't do the job.


To access the declared constant value, use "self~typs".
0 new messages