yourself

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

yourself

Bakki Kudva
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
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: yourself

cedreek
I have   ^self in my image
but anyway, by default, if there is ^ in a method, it returns self...
so an empty method (message) actually return self...

Cédrick

2006/8/31, Bakki Kudva <[hidden email]>:

> 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
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: yourself

Marcus Denker
In reply to this post by Bakki Kudva

On 31.08.2006, at 17:35, Bakki Kudva wrote:

> 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?
>

self is the default return value. So if you return nothing special,  
the compiler
will add a ^self at the end.

A good rule is to make the ^self explicit as soon as it is important,  
thus
for our lecture (#yourself is covered in 04:
http://www.iam.unibe.ch/~scg/Teaching/Smalltalk/Slides/04Idioms.pdf )

we changed the implementation of #yourself to be

yourself
        "Answer self."
        ^self

This is in 3.9

     Marcus
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: yourself

cedreek
In reply to this post by cedreek
I forgot **no**

if there is no $^  in a method ....
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: yourself

Eric Winger
In reply to this post by Bakki Kudva

On Aug 31, 2006, at 8:35 AM, Bakki Kudva wrote:

> 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?

By default, all message sends return an object. If no object is
specified by ^, then self is returned.

Eric

>
> Thanks,
>
> bakki
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: yourself

Bakki Kudva
In reply to this post by Marcus Denker
Cédrick, Marcus,

Thank you so much for clarifying this for me. I downloaded the
lectures and they look great. I am sure I'll learn a lot from them. If
it is not asking for too much it would  be aweswome if there were mp3
audio files of the lectures to go with the slides. I downloaded the
Abelson Sussman lectures...
http://www.swiss.ai.mit.edu/classes/6.001/abelson-sussman-lectures/
which are fantastic as well...but for Scheme mostly.

Thanks again,

bakki

On 8/31/06, Marcus Denker <[hidden email]> wrote:

>
> On 31.08.2006, at 17:35, Bakki Kudva wrote:
>
> > 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?
> >
>
> self is the default return value. So if you return nothing special,
> the compiler
> will add a ^self at the end.
>
> A good rule is to make the ^self explicit as soon as it is important,
> thus
> for our lecture (#yourself is covered in 04:
> http://www.iam.unibe.ch/~scg/Teaching/Smalltalk/Slides/04Idioms.pdf )
>
> we changed the implementation of #yourself to be
>
> yourself
>         "Answer self."
>         ^self
>
> This is in 3.9
>
>      Marcus
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

RE: yourself

Ron Teitelbaum
In reply to this post by Bakki Kudva
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
[hidden email]


> -----Original Message-----
> From: [hidden email] [mailto:beginners-
> [hidden email]] 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
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: yourself

Marcus Denker
In reply to this post by Bakki Kudva

On 31.08.2006, at 18:01, Bakki Kudva wrote:

> Cédrick, Marcus,
>
> Thank you so much for clarifying this for me. I downloaded the
> lectures and they look great. I am sure I'll learn a lot from them.

A link with all the slides (and exercises) is available here:

http://www.iam.unibe.ch/~scg/Teaching/Smalltalk/index.html

> If
> it is not asking for too much it would  be aweswome if there were mp3
> audio files of the lectures to go with the slides. I downloaded the
> Abelson Sussman lectures...
> http://www.swiss.ai.mit.edu/classes/6.001/abelson-sussman-lectures/
> which are fantastic as well...but for Scheme mostly.
>

I know... videos are a very nice thing. But we don't have any of the
SCG Smalltalk lecture...

There are some Smalltalk/Squeak related videos on Google:

http://video.google.com/videosearch?q=label:esug

e.g. Dan Ingall's classic video about OO, some talks about Seaside,
lots of cool Alan Kay talks (I love the OOPSLA97 keynote...).

    Marcus_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: yourself

Bakki Kudva
In reply to this post by Ron Teitelbaum
Hi Ron,

I was reading the section on yourself in Kent Beck's Best Practices
Patterns book. Your examples made it even more clearer how to use
cascading with yourself Thanks a lot.

bakki

On 8/31/06, Ron Teitelbaum <[hidden email]> wrote:
> 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.
<snip>
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners