On Fri, Jul 30, 2010 at 6:51 AM, Anderson Goulart <[email protected]> wrote:
> Reading the manual, I saw that we can create object attrs using expose > inside the method, but you actually can create them using ::attribute. Is > that right? Any differences between them? There are subtle differences. And this is an area that has always given me trouble, so I'm not sure if my explanation will be the best. expose doesn't create an attribute, it creates an instance variable. The ::attribute directive also creates an instance variable, but it also creates two accessor methods, in the normal usage. One to set the instance variable and one to get the instance variable. (Although the ::attribute directive also takes keywords that can alter this.) Instance variables are only visible within the instance object itself, whereas attribute methods are / can be visible outside of the instance object. These are subtle differences, to me, and took me awhile to grasp. > Like these examples: > > /* first example */ > ::class Example > > ::method init > expose sample > sample = 0 > > > /* second example */ > ::class Example > > ::attribute sample private > > ::method init > self~sample = 0 > > > It is not clear for me if expose lets "sample" attr become private, > protected or public. The expose is very much private, but actually even move restrictive. Take these two examples, roughly based on what you posted. /* First example */ obj = .GoodExample~new obj~makeValid say "Is obj valid?" obj~isValid obj~makeInvalid say "Is obj invalid?" obj~isValid ::class Example public ::method init expose valid valid = .false ::method makeValid expose valid valid = .true say "Made this object valid" ::method makeInvalid expose valid valid = .false say "Made this object invalid" ::class GoodExample subclass Example ::method init self~init:super ::method isValid expose valid return valid When run from the command line you will see: C:\work\wc>qTest.rex Made this object valid Is obj valid? VALID Made this object invalid Is obj invalid? VALID C:\work\wc> That's not the output I was expecting when I first started programming in ooRexx. On the other hand, this second example does work as I first expected: obj = .OtherExample~new obj~makeValid say "Is obj valid?" obj~isValid obj~makeInvalid say "Is obj invalid?" obj~isValid ::class ExampleTwo public ::attribute valid private ::method init expose valid valid = .false ::method makeValid expose valid valid = .true say "Made this object valid" ::method makeInvalid expose valid valid = .false say "Made this object invalid" ::class OtherExample subclass ExampleTwo ::method init self~init:super ::method isValid return self~valid The output is: C:\work\wc>qTest2.rex Made this object valid Is obj valid? 1 Made this object invalid Is obj invalid? 0 C:\work\wc> So, the key thing is that when you use expose to create an instance variable, 'valid' in this case, it is only visible in the direct instance object. In the first example, even though the GoodExample object is an Example object, the 'valid' instance variable in Example is not visible in the GoodExample object. Whereas in the second example, by using the ::attribute directive, you give subclass objects a way to access the 'valid' instance variable, even though the attribute is private. -- Mark Miesfeld ------------------------------------------------------------------------------ The Palm PDK Hot Apps Program offers developers who use the Plug-In Development Kit to bring their C/C++ apps to Palm for a share of $1 Million in cash or HP Products. Visit us here for more details: http://p.sf.net/sfu/dev2dev-palm _______________________________________________ Oorexx-users mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/oorexx-users
