[Newbies] yourself

Ron Teitelbaum Ron at USMedRec.com
Thu Aug 31 16:04:28 UTC 2006


Hi Bakki,

yourself is used quite often for chaining messages.  Long chains are
definitely bad form but in some cases you are stuck without having yourself.

Take for example 

	aCollection add: 'hello'

When you first encounter this message many people make the mistake of
believing that add: returns a collection.  You will see bugs created in this
way.  For example someone might write

	"THIS CODE DOES NOT WORK"
	aCollection := aCollection add: 'hello'.

Since after running this code what you get is aCollection = 'hello'.
(Forget that the assignment isn't even necessary for the moment since
aCollection is changed with add, you will still see this).

The result is not what you would expect.

	To fix this you do:

	"THIS CODE DOES WORK"
	aCollection := aCollection add: 'hello'; yourself.

This basically returns the "self" of the previous message instead of the
value that is returned from the previous message.

Also consider this

	MyClass class > createFrom: anOtherObject
		"return to the sender an instance of the receiver setting
anOtherObject to someOtherObject"
		^(MyClass new)
			someOtherObject: anOtherObject 

if the mutator returns self you are ok if not this will not work.  It
usually does, but other methods may not for example: 

	MyClass class > createFrom: anOtherObject
		"return to the sender a processed instance of the receiver
setting anOtherObject to someOtherObject"
		^(MyClass new)
			someOtherObject: anOtherObject;
			processResult

if processResult sets a value on the object and returns the result then you
are stuck.  Fix it with yourself.

	MyClass class > createFrom: anOtherObject
		"return to the sender a processed instance of the receiver
setting anOtherObject to someOtherObject"
		^(MyClass new)
			someOtherObject: anOtherObject;
			processResult;
			yourself.


Hope that helps!

Happy Coding

Ron Teitelbaum
President / Principal Software Engineer
US Medical Record Specialists 
Ron at USMedRec.com 


> -----Original Message-----
> From: beginners-bounces at lists.squeakfoundation.org [mailto:beginners-
> bounces at lists.squeakfoundation.org] On Behalf Of Bakki Kudva
> Sent: Thursday, August 31, 2006 11:36 AM
> To: A friendly place to get answers to even the most basic questions
> aboutSqueak.
> Subject: [Newbies] yourself
> 
> Hi,
> 
> I'm a bit confused about y ourself. I understand that the definition
> of yourself is ^self
> But when I browse Object and look at yourself: all I find is the
> description string..
> "Answer self."
> 
> What happened to ^self? Where is the actual code implementing this?
> 
> Thanks,
> 
> bakki
> _______________________________________________
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/beginners




More information about the Beginners mailing list