Shop Learn P1 Docs P2 Docs Events
Spin2 language proposal: anonymous objects — Parallax Forums

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

  • ersmithersmith Posts: 5,618

    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.

Sign In or Register to comment.