Chaining problem

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

Chaining problem

Jakub-8
Hello
can anybody please explain me this example:

in init i make:
left := OrderedCollection new.
left add: nil -> 'Eukaryota'.
left add: 'Eukaryota' -> 'Opisthokonts'.

and then in another method:
(self perform: #left) do: [ :item | ...]


it's ok, but if i change init to
left := OrderedCollection new add: nil -> 'Eukaryota'; add: 'Eukaryota' -> 'Opisthokonts'.

and then i call
(self perform: #left) do: [ :item | ...]
i get error

i thought both init are equivalent

thnx.





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

Re: Chaining problem

Lukas Renggli
> can anybody please explain me this example:

I sort of recognize this code to be simliar to something I wrote a
long time a go (SUTreeTest in the Scriptaculous package). It would
help us to help you, if you stated where exactly you found this
example and what you changed. See
http://www.seaside.st/community/mailinglist for more information on
asking questions efficiently.

> in init i make:
> left := OrderedCollection new.
> left add: nil -> 'Eukaryota'.
> left add: 'Eukaryota' -> 'Opisthokonts'.

What is #init? Shouldn't this be called #initialize? Where did you
define this method? SUTreeTest?

>  and then in another method:
> (self perform: #left) do: [ :item | ...]

Why not write (self left) instead of (self perform: #left)?

> it's ok, but if i change init to
> left := OrderedCollection new add: nil -> 'Eukaryota'; add: 'Eukaryota' ->
> 'Opisthokonts'.
>
> and then i call
> (self perform: #left) do: [ :item | ...]
> i get error

You need #yourself at the end of the cascade, because #add: usually
answerss the added element. See page 213 'A common mistake with add:'
in 'Squeak by Example' (http://www.iam.unibe.ch/~scg/SBE/SBE.pdf).

Lukas

--
Lukas Renggli
http://www.lukas-renggli.ch
_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
Reply | Threaded
Open this post in threaded view
|

Re: Chaining problem

Claus Kick
In reply to this post by Jakub-8
Hello,

this is a very common cascading error.

Have a look at the #add: implementation and you should see that the argument is what is being returned by the selector, not the receiver.

I have not yet understood why this is the case, simply not returning anything which then returns self (the receiver) would be easier to understand and
what you want to have anyways.

What is more, you should also be able to do

left := OrderedCollection with: nil -> 'Eukaryota' with: 'Eukaryota' -> 'Opisthokonts'.

That depends on the Smalltalk dialect, though.

As it is a common mistake, I tend not to use cascading at all.
What is more, in our work environment, cascading tended to be a bit slower than using normal message sends (for whatever reason).

Claus

> -----Ursprüngliche Nachricht-----
> Von: Seaside - general discussion <[hidden email]>
> Gesendet: 30.12.07 11:49:33
> An: "Seaside - general discussion" <[hidden email]>
> Betreff: [Seaside] Chaining problem

Hello

> can anybody please explain me this example:
>
> in init i make:
> left := OrderedCollection new.
> left add: nil -> 'Eukaryota'.
> left add: 'Eukaryota' -> 'Opisthokonts'.
>
> and then in another method:
> (self perform: #left) do: [ :item | ...]
>
>
> it's ok, but if i change init to
> left := OrderedCollection new add: nil -> 'Eukaryota'; add: 'Eukaryota' -> 'Opisthokonts'.
>
>
> and then i call
> (self perform: #left) do: [ :item | ...]
> i get error
>
> i thought both init are equivalent
>
> thnx.
>
>
>
>
>
> -----------------------------------------------------------------
> _______________________________________________
> seaside mailing list
> [hidden email]
> http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
>

--
Claus Kick

"Wenn Sie mich suchen: Ich halte mich in der Nähe des Wahnsinns auf.
Genauer gesagt auf der schmalen Linie zwischen Wahnsinn und Panik.
Gleich um die Ecke von Todesangst, nicht weit weg von Irrwitz und Idiotie."

"If you are looking for me: I am somewhere near to lunacy.
More clearly, on the narrow path between lunacy and panic.
Right around the corner of  fear of death,
not far away from idiotism and insanity."
_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside