Returning a reference to 'this'
Mightor
Posts: 338
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).
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
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
| 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
[noparse]:)[/noparse]
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.·
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
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
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
·
One of the "best" examples in C++ is the use of the 'cout' object:
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
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.