Shop OBEX P1 Docs P2 Docs Learn Events
Ability to share objects among other objects — Parallax Forums

Ability to share objects among other objects

rokickirokicki Posts: 1,000
edited 2006-12-15 22:15 in Propeller 1
I've created the wiki page

http://scratchpad.wikia.com/wiki/PropellerSpinEnhancements

containing a simple idea on how we might share objects among other objects with a
relatively simple change to Spin.

I'm hoping others will edit this page to improve or change the suggestion. (Wikis are
great fun.)

I'm posting the current text here just for kicks.

This is just off the top of my head and is far from a serious, thought-out proposal, but
I thought I'd get the ball rolling.

Here are some ideas for enhancements to Spin, the Propeller Programming Language created by Parallax.

Permit objects to be shared between other objects.

Define "template" objects to permit such sharing.

For example, let's say that I want to write a program consisting of an object hierarchy where A is my top-level object, and it uses objects B and C and D. All of A, B, C, and D want to do general I/O using a keyboard/screen or serial terminal or whatnot. But it's critical that they all share the same I/O object (in the physical sense).

At the same time, the flexibility to change from a serial interface to a keyboard/TV or keyboard/VGA interface, with only one or two lines of code, is important.

To make this work, we add the ability to define an object template. An object template is just like any other object, containing public methods, but the actual code and data of that object is typically just simple stubs. For instance, a generic I/O genio.spin object might just be

PUB getchar
repeat
' do nothing
PUB putchar(n)
PUB charready
return 0

The idea here is that any object that has methods with these three signatures can be "plugged in" where the template object is used. The substitution of the objects is done at the object instantiation.

So for instance objects B, C, and D all might use

OBJ
generic_io : "genio"

A will create B, C, and D normally with code like

OBJ
My_b_obj : "B"
My_c_obj : "C"
My_d_obj : "D"

But, A also has the option of "rebinding" or "renaming" the objects accessed by sub-objects. For instance, it might say

OBJ
RealIO : "KeyboardPlusVGA"
My_b_obj : "B"("genio"/RealIO)
My_c_obj : "C"("genio"/RealIO)
My_d_obj : "D"("genio"/RealIO)

which means, when including the "A" object, replace any occurrence of "genio" throughout B, C, or D with the specific RealIO object that is created at the top level of A. In this case we only instantiate RealIO *once*, but it is accessible to B, C, and D.

In addition, the B, C, and D objects are all separately usable using whatever I/O they may have defined.

Note that we do not "check" anywhere that RealIO contains all the methods defined in genio; that is done when the top-level object is compiled (and it will recompile the subobjects, with the appropriate substitution of "objects").

We also need a method for conditional compilation and the like, but this is just an idea off the top of my head. Feel free to edit.

(Initial version by rokicki)

Comments

Sign In or Register to comment.