Project Updates

Saturday, January 29, 2011

Added shared members

Finally got around to adding this feature which will eliminate an ugly hack in iEntity.f, and I think actually eliminate iEntity altogether?  (Well, we'll see.)

Shared members allow one object to share properties with another one, so that they appear to be properties of the sharee, but they really are the sharer's.

The way it turned out to be implemented was surprisingly elegant.

To use it, you create a special property called a "reference" with the word "Via".

Example:

Module Entity
Via Owner

This makes a field called Owner for Entity that works like any other property.  There is nothing special about it by itself.  But now, all subsequent fields will become shared fields using that reference Owner.

Vector Make Pos
Var Visible
...
(And so on.  To get out of that mode, you'd call Static, Embedded, or Global)

So now, instead of pointing to This like most fields, Pos and Visible would go through the address pointed to by Owner.  To use them, you have to set Owner to something before doing anything.  This might be made automatic for the sharer sometime, but for now, you have to set it manually in both constructors.

To share those fields, here's what you'd do:

Component MyComponent  \ As an example, we're using a component, but you can do it with any Structure or Module too.
Using Entity Share Owner

Actually, Component will define its own Owner automatically, but for the sake of this example we're making it explicit.

First we call Using to add Entity's words to MyComponent's included classes.  Then we say we want to share properties associated with the Owner reference.  (What's kind of cute is that Share is nothing but an alias for Var.)  So now, the Owner's Pos and Visible can be called from MyComponent's functions as if they were part of MyComponent.  Even >Pos and >Visible work.

What's great about the implementation is references don't have to be properties of classes; they can be static, or even global.  All the shared properties do is evaluate "Owner @" and then the property as normal.  You can connect lots of interdependent objects without explicit coupling throughout the code; the connection is implicit and defined in one place.

No comments:

Post a Comment