|
RE: yourself: msg#00269lang.smalltalk.squeak.beginners
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@xxxxxxxxxxxx > -----Original Message----- > From: beginners-bounces@xxxxxxxxxxxxxxxxxxxxxxxxxx [mailto:beginners- > bounces@xxxxxxxxxxxxxxxxxxxxxxxxxx] 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@xxxxxxxxxxxxxxxxxxxxxxxxxx > http://lists.squeakfoundation.org/mailman/listinfo/beginners
|
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | I am not sure if choose squeak/smalltalk or another language. HELP, Antonio San. |
|---|---|
| Next by Date: | Re: yourself, Marcus Denker |
| Previous by Thread: | Re: yourself, Eric Winger |
| Next by Thread: | Re: yourself, Bakki Kudva |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |