Spin2 language proposal: anonymous objects
Basically, it's a way to access a sub-object without having to name it, or equivalently to directly include the sub-object's methods and variables into the parent object. It's easiest to give an example; let's suppose we have a Serial object with methods startx() and str() that we want to include in our program:
OBJ _ : "Serial" ' using the placeholder _ as name includes Serial as an anonymous object ' now Serial's methods may be accessed directly PUB main() startx() ' calls Serial.startx str(msg) ' calls Serial.str(msg)
This serves two purposes:
(1) Like Pascal's WITH or Python's FROM ... IMPORT, it lets you refer to the sub-object methods without having to name the sub-object, removing a bit of syntactic clutter.
2) It provides a kind of simple inheritence scheme for objects.
Implementation-wise, it could be done in a number of ways. A simple one might be to just auto-generate methods for the parent, i.e. the above snippet would be exactly equivalent to:
OBJ __anon__ : "Serial" PUB startx() __anon__.startx() PUB str(s) __anon__.str(s) PUB main() startx() str(msg)
A slightly more sophisticated implementation would re-write the method calls, so references to the object's startx
method would automatically be replaced by __anon__.startx()
.
It'd be perfectly reasonable to allow only one anonymous sub-object to be present in any given parent object.
@cgracey, thoughts?
Comments
Crickets? I guess this feature isn't too interesting, so I haven't documented it, but flexspin 5.9.25 does implement it.
This sounds like it would be good for things to seem like they're more "built-in", like a string manipulation object (C standard lib?) or something. You'd get the benefit of it being a reusable object, but it'd seem more transparent.
@ersmith
This is interesting. It reminds me of the concept of interfaces in Java, except that the implementation is done in the external object. In your suggested implementation if you are auto-generating the object declarations, would it be too difficult to generate unique object declarations for each anonymous object declared, assuming they are from different object files?
(i.e. multiple interfaces support)
I could see the use case for this feature in my GUI framework where I have "widgets" and some of them share some functionality like responding to touch events so for these I could have a
Pressable
interface object that provides the methods for responding to touch events and anotherIndicator
object to provide the methods related to changes in appearance based on setpoint values. By declaring these anonymous objects I would be "implementing" these interfaces in a particular widget.