DependentsArray size how does it work ?

nicolas cellier ncellier at ifrance.com
Wed Feb 8 12:58:40 UTC 2006


Le Mercredi 08 Février 2006 09:46, vous avez écrit :
> On 08.02.2006, at 04:38, nicolas cellier wrote:
> > 1) in squeak you can overwrite block arguments (storeIntoTemp: 0),
> > in not many
> > Smalltalk you can do that i guess (not in VW anyway).
> > In my opinion this is never something to do.
>
> Yes, evil.
>
> > 2) there must be a trick somewhere...
...
> > Then, on second pass, shouldn't the block evaluate with: count:nil
> > dep:$0 ?
> > Then, we would have count := nil+1 that would fail, that's what i
> > imagined...
> >
> > Not at all, all is going fine. Can anybody teach me ?
>
> The do: is reimplemented, too. And inject:into: uses do:.
> The do: now filters for nil, thus the count:0 dep:nil will not execute
> anything.
>

Thank you Marcus. A good lesson to me.

I did not search in the right place, sticking to this ifNotNil: that prevented 
me to go further...

> And as dep can never be nil, the method could, I think,  look like this:
>
> size
>  ^self inject: 0 into: [ :count :dep |  count + 1 ]
>
>      Marcus

Waouh, very nice. But i am afraid i would not understand such a code neither 
Pretending i do not know of the do: re-implementation trick, i would not see 
why this size should be different from super size.

I did not understand how first implementation did work, but i understood 
programmer's intention.
I used this example to propose a clean understandable collection extension:
    ^self count: [:e | e notNil]

What you're proposing above sounds a little bit like:
    ^self count: [:e | true]

Apart writing the block argument, all code above is correct, tricky but 
correct.
At least, there is a programmer that can be proud driving me nuts!
Tricky code deserve either refactoring or implementation notes i think.

By the way, is the #at: message also redefined ? (did not check yet).
If not, following (bad style) code could be broken with DependentsArray :
    1 to: someDependents size do: [:i | (someDependents at: i) doSomething]

I will try it this evening. if not redefined, then the whole class seems 
dangerous to me and there might be other side effects.




More information about the Squeak-dev mailing list