RE: [Newbies] Should all methods return a value?
From: Charles D Hixson Sent: Tuesday, May 09, 2006 1:16 AM I know that in some languages this matters, and in others it doesn't. If a method doesn't explicitly have a return value it will return self, but you should never count on that, small changes to the method will have a tendency to break your code. Especially when someone decides that the method should return something like true or false. If in your code you really want self back from a method you can protect yourself from this problem by adding ; to the end of your method call. For example: aFireman putOutFire may return nothing in which case the following would work since it returns self. aFireman putOutFire cleanUpMess. But if someone decides that putOutFire should return #smokeDamage then you would be in trouble. So it's better to write the code aFireman putOutFire; cleanUpMess. This says send the next message to the receiver object of the previous message and it translates as aFireman putOutFire. aFireman cleanUpMess. And if you haven't guessed aFireman putOutFire; cleanUpMess; driveBackToStation. Works too. Also sometimes what is returned can get in the way and if you really need to return self you should add the following. aFireman putOutFire; yourself. This way you could guarantee that the right object self ends up where it should. For example aFireStation medalCandidates add: (aFireman putOutFire; yourself). This makes sure that self is returned and changes to the implementation of putOutFire will not hurt your code. This is especially useful in collections, since add returns the object that was added and not the collection. aCollection := aCollection add: aFireman would set aCollection to aFireman, not what was intended. But aCollection := aCollection add: aFireman; yourself. Returns aCollection with an additional fireman in it. Hope that helps! Happy Coding!! Ron Teitelbaum President / Principal Software Engineer US Medical Record Specialists [EMAIL PROTECTED] ___ Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners
Re: [Newbies] Should all methods return a value?
Yoshiki Ohshima wrote: Charles, Todd Blanchard wrote: The rule is, if you don't return a value, then self is returned. There's no such thing as a void message like in C++ or Java. Tell me what you want to do and I'll see if I can scare up some examples. On May 8, 2006, at 10:16 PM, Charles D Hixson wrote: I know that in some languages this matters, and in others it doesn't. ... ... What Todd meant to say was: if you don't *explicitly* return a value with a '^' statement, the receiver (self) is returned. Basically, all message-sending return some values. Returning self is fine. I just wanted to know what would happen, so I could do things properly. (Actually, right not the methods would execute Object shouldBeImplemented, so they probably won't really return anything...but I was trying to plan for the future.) I don't know if the following is relevant what you do, but here is a little fun fact. Almost all errors and explicit runtime exceptions like #shouldNotImplemented are decorated break points. If you push the Proceed button in the pink window called notifier, the execution continues. Since ObjectshouldBeImplemented is implemented as: - shouldBeImplemented Announce that this message should be implemented self error: 'This message should be implemented' - without any explicit return, the receiver is returned and the execution continues. Try an expression like following, evaluate the expression and proceed. -- Transcript show: (3 shouldBeImplemented + 4) printString. -- -- Yoshiki Good. That is exactly the way I would want it to have been designed, now that I think about it. ___ Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners
Re: [Newbies] Should all methods return a value?
The rule is, if you don't return a value, then self is returned. There's no such thing as a void message like in C++ or Java. Tell me what you want to do and I'll see if I can scare up some examples. On May 8, 2006, at 10:16 PM, Charles D Hixson wrote: I know that in some languages this matters, and in others it doesn't. E.g., in C if you return a value, the temporary must always be stored when the function is called, but in other languages it can be ignored, and in some languages if you don't intentionally return a value, the system will automatically create a value (usually nil) to return for you. Eiffel make a big deal about separating Commands from Queries, with very strict rules about which kinds of routines can do what kind of things. Presumably Squeak (and Smalltalk) is flexible here...but I haven't seen it documented anywhere. (Also, can anyone recommend a good tutorial that isn't graphics based? What I want to do right now is really more text oriented.) I got the two Mark Guzdial books, but that doesn't really cover much in any depth. ___ Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners ___ Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners