OOP in Rev...

Trevor DeVore lists at mangomultimedia.com
Thu Mar 20 12:02:45 EDT 2008


On Mar 19, 2008, at 8:25 AM, viktoras didziulis wrote:

> I would like to define a class, an object (the both with their  
> properties) and methods that are not a part of graphical user  
> interface or Revolution engine...
>
> Let's say I need an object "City" having properties: minLatitude,  
> maxLatitude, minLongitude, maxLongitude, area, populationSize,  
> growthRate, dateFounded, currentDate so it can be handled using  
> specific handlers and functions (e.g. its methods). Or, well, it  
> would be more correct to start with creation of a class "City" or  
> "templateCity" and then use it to create new "city" objects  
> (NewYork, Paris, etc...) that can be processed by their functions  
> like "populationGrowth(Paris, start_year, end_year)" and so on...
>
> The three approaches that I can imagine are somehow inter-tangled:  
> (1) creating an object as a custom property set of a stack or (2)  
> creating it as an array or (3) creating it as an invisible control  
> with custom properties. But all these have their drawbacks.

Just some quick notes from methods I've tried. You probably won't find  
any consensus on this since Rev does not have an elegant way of  
extending the xtalk syntax to our own custom objects. Hopefully one  
day...

1) This could work but I have found it easier to work with individual  
objects. I just pass button references around to my "class" handlers.  
See point 3 notes.

2) This can be useful in some circumstances but it is easier to  
inspect the custom props of a button during debugging.

3) I prefer this approach. I create a stack that I use as a library. I  
then create "classes" using handler prefixes:

put city.getProp (pLongIDofButton, "growth rate") into theGrowthRate
city.setProp pLongIDofButton, "growth rate", "1"
put city.Serialize (pLongIDofButton) into theSerializedCity

Each handler takes a reference to a button whose uObjectProps custom  
property set represent the object properties. So calling city.getProp  
would return the uObjectProps["growth rate"] custom property for  
example. Calling city.setProp would set the uObjectProps["growth  
rate"] value after having performed integrity checks.

I create buttons on hidden stacks or just hide the button itself on a  
visible stack. Depends on my needs.

I prefer to define city.getProp/setProp handlers since I hate writing  
out city.getGrowthRate, city.setGrowthRate every time I want to add a  
new property.

I never use Revolution's getProp/setProp handlers when dealing with  
custom objects unless they are GUI elements. getProp/setProp fails to  
be called when messages are locked which is not very robust.

> The (1) and (3) approach allows attaching handlers to custom  
> properties and allows accessing object (e.g. a custom property set)  
> properties using both an array notation (somehow an equivalent to an  
> array-like behavior of objects in javascript) and in a way  
> consistent with handling of properties in Rev e.g. "set the .. of ..  
> to ..." or "get .. the .. of ...". Unfortunately (1) allows only a  
> single object to be active and thus accessible. The (3) is a dirty  
> one, because object is created using empty controls with their own  
> additional properties and methods. The (2) looks promising, one can  
> create a class, write a constructor function that would create new  
> objects from the class, etc... But it lacks consistency with the  
> existing OOP style in Rev - e.g. you can not get or set an element  
> of an array using "get the <element/property> of <array/object>" or  
> "set the <element/property> of <array/object> to <value>". Besides  
> you can not use getProp or setProp handlers with array's elements.

Not being able to define our own objects that have the same elegant  
syntax that is native to Rev is the real drawback.

> Did anyone try doing something like this kind of OOP in Rev? I would  
> appreciate if you can share your thoughts, warnings, tricks and  
> approaches :-). I am not looking for a complex C++ like style of OOP  
> in Rev. Anything simpler like the OOP model of javascript would be  
> OK. In general, I think, it would be nice if one could treat and  
> access Revolution arrays as objects with custom properties, with all  
> the getProp, setProp and templateObject stuff... Or is it possible  
> somehow? If not, is this already posted to QC as an enhancement  
> request - I would vote for it? Otherwise going to post it there  
> myself...

Just some thoughts. I have tried a few different approaches myself and  
the above is what I have settled on for the time being.

Regards,

-- 
Trevor DeVore
Blue Mango Learning Systems
www.bluemangolearning.com    -    www.screensteps.com




More information about the Use-livecode mailing list