Shop OBEX P1 Docs P2 Docs Learn Events
Returning a reference to 'this' — Parallax Forums

Returning a reference to 'this'

MightorMightor Posts: 338
edited 2007-08-06 21:00 in Propeller 1
Hey there,

I am fiddling about with the SimpleDebug Object I pulled from the Object Exchange. What I'd like to do is allow for a construction like this: (.nl is a new function I made that prints a new line & carriage return).

OBJ
  Debug : "SimpleDebug"

PUB Howdy
  Debug.start(57600) 
  Debug.str(string("Hello World")).nl




However, that would require the Debug object being able to return a reference to itself, a bit like C++'s 'return this;'. Is this even possible in SPIN?

Gr,
Mightor

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
| What the world needs is more geniuses with humility, there are so few of us left.
| "Wait...if that was a compliment, why is my fist of death tingling?"
| - Alice from Dilbert

Comments

  • deSilvadeSilva Posts: 2,967
    edited 2007-08-05 19:47
    ROFL
  • MightorMightor Posts: 338
    edited 2007-08-05 20:05
    deSilva said...
    ROFL
    Oh dear. Is that a no then? [noparse]:)[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    | What the world needs is more geniuses with humility, there are so few of us left.
    | "Wait...if that was a compliment, why is my fist of death tingling?"
    | - Alice from Dilbert
  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-05 20:41
    That's a no. Objects in Spin are very limited. They're essentially a namespace with a static per-instance variable space. The variables are known only within the object source file except that named constants and procedures/functions (methods) marked public (PUB) are accessible from outside as long as they have an instance prefix. Multiple instances use shared code and shared data (DAT).
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-08-05 21:33
    Bad deSilva, bad!

    [noparse]:)[/noparse]
  • mirrormirror Posts: 322
    edited 2007-08-05 21:49
    Mightor,

    If you're wanting a serial port that can be accessed from multiple spin files, then try SerialMirror:

    http://forums.parallax.com/showthread.php?p=649541

    The concepts can also be used to make other objects visible/useable in multiple spin files.·
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-08-06 00:10
    Mightor,

    I'm not completely sure what you're trying to do (since I'm not a C++ programmer), but I suspect my callback facility might be of some use. Anyway, you can check it out here:

    ····http://forums.parallax.com/showthread.php?p=593662

    It breaks some rules and needs to be used with caution, but it may prove useful, nonetheless.

    -Phil
  • MightorMightor Posts: 338
    edited 2007-08-06 05:46
    What I am trying to do is this. In C++ and many other OO languages it is possible for an instance of an object to return a reference to itself. This allows you to chain commands using the . operator. Say you have an object instance named foo with a member function called baz. The baz function, besides doing some operations, also return a reference to the foo instance.
    Calling foo.baz() would then return a reference to foo again. This would allow for something like foo.baz().baz(). This can be very handy in some cases.

    Phil,
    Hehe, your code intimidates me a little and if it takes voodoo magic to accomplish something like this then it's not really worth it for me. I would probably spend more time trying to figure out a bug I caused trying to copy your code appropriately than actually learning to use this SPIN language. I am still quite new to it, as I've only had my board since last Friday or so.

    Mirror,
    I had seen posts of yours before mentioning the SerialMirror object, I will check it out later [noparse]:)[/noparse] Is there any chance you can upload it to the CodeExchange site? That will give it a lot more exposure, too. You can find that here: http://ww1.parallax.com/SupportDownloads/PropellerLibrary/tabid/65/Default.aspx, I have no idea what is involved in getting access to it, but I am sure it is mentioned on there somewhere.

    I think for now I will just stick with two lines of code [noparse]:)[/noparse]

    Gr,
    Mightor

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    | What the world needs is more geniuses with humility, there are so few of us left.
    | "Wait...if that was a compliment, why is my fist of death tingling?"
    | - Alice from Dilbert
  • BeanBean Posts: 8,129
    edited 2007-08-06 11:32
    Mightor,
    Hey it was a good idea. But what spin calls an object is not what a OOP language calls an object. I'd say it's more like a library. So you cannot do polymorphism or anything like that with the normal spin "objects".

    I have never seen that used, but I'm a delphi programmer. I'll have to try that in delphi and see if it works. Thanks for the insight.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Teacher: What is the difference between ignorance and apathy ?
    Student: I don't know and I don't care
    Teacher: Correct !
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    www.hittconsulting.com
    ·
  • MightorMightor Posts: 338
    edited 2007-08-06 11:58
    Bean (Hitt Consulting) said...
    Mightor,
    Hey it was a good idea. But what spin calls an object is not what a OOP language calls an object. I'd say it's more like a library. So you cannot do polymorphism or anything like that with the normal spin "objects".

    I have never seen that used, but I'm a delphi programmer. I'll have to try that in delphi and see if it works. Thanks for the insight.

    Bean.
    Oh I wasn't looking to get as fancy as polymorphism, just a little reference to itself would've made me happy, haha.
    One of the "best" examples in C++ is the use of the 'cout' object:
    cout << "The value of foo is: " << foo << endl;
    
    


    The << operator is overloaded and returns a reference to the cout object, allowing for the next << operator to make use of it [noparse]:)[/noparse] Looks simple and beautiful (in my eyes, that is). You can do this kind of thing in VB, C++ and probably Java, too. I have no idea if Delphi can do this, as I have never programmed that.

    Gr,
    Mightor

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    | What the world needs is more geniuses with humility, there are so few of us left.
    | "Wait...if that was a compliment, why is my fist of death tingling?"
    | - Alice from Dilbert
  • deSilvadeSilva Posts: 2,967
    edited 2007-08-06 21:00
    Returning function pointers is a piece of (non OOP) cake, IF you have the concept of function pointers at all. SPIN has not.

    What I thought Mightor was looking for in his OP, was adding methods to an object by formal inheritance as in C++ or Java, or some ad-hoc means as available in JavaScript, PHP 4, etc.
Sign In or Register to comment.