[squeak-dev] String >> #numArgs

Thiede, Christoph Christoph.Thiede at student.hpi.uni-potsdam.de
Mon Dec 16 13:40:32 UTC 2019


Hi Tobias, thanks for your input!


> Why not simply

>         Matrix rows: 5 columns: 6 tabulate: [:x :y | x @ y]
>
> What's wrong with blocks?

At least in scripting scenarios, they cost extra redundant characters. Symbol >> #value:value: is a known selector (at least now), so why should I always type all these redundant letters?

> A symbol should not answer to an #value: … #value:value:...

Levente should know it, he implemented Symbol >> #value:value:.
However we solve this, I think, at the moment, we have an irritating inconsistency. Imho, Symbol >> #value: is not better than Symbol >> #value:value at all. And I use it every day, so I would vote for increasing the support. (BalloonMorph allInstancesDo: #abandon, SystemWindow select: #isInWorld, ...)

> I would then rather use withIndexCollect. It does what it says on the tin.

At the moment, we have #collect: and #withIndexCollect: side by side. However, their implementations are almost equal, and my problem is that every method that I write to access a collection (e. g. collection accessors) can only support one of them. Why not exploit the fact that [:x :i | x + i] and [:x | x squared] can be distinguished by #numArgs and unify these two accessors?

> you can use cull:cull: anyways.

Interesting point. This would also simplify the approaches for our recent #to:collect: issue:
(1 to: 5) collectX: [Color random]

Could we maybe just adapt SequenceableCollection >> #collect: to support 0 - 2 block arguments instead of exactly one? It would be nice to have this in Trunk!

> > Symbol >> #asBlock
> that's not too bad :D
> but there is already perform:withArguments:, so this should be simpler?

I would be glad to send something to the Inbox. But how do you create a block by reflection, without knowing the number of arguments?
It looks as if there is no public interface yet and I would have to generate the byte codes manually. Or am I overlooking something?

Best,
Christoph

________________________________
Von: Squeak-dev <squeak-dev-bounces at lists.squeakfoundation.org> im Auftrag von Tobias Pape <Das.Linux at gmx.de>
Gesendet: Montag, 16. Dezember 2019 13:41:03
An: The general-purpose Squeak developers list
Betreff: Re: [squeak-dev] String >> #numArgs

Hi

> On 16.12.2019, at 13:18, Thiede, Christoph <Christoph.Thiede at student.hpi.uni-potsdam.de> wrote:
>
> Hi all! :-)
>
> > Woah! This works? When and why is this useful? Does anybody recall the prime example? :-) #inject:into:?
>
> Yes, I believe I once used it with #inject:into:, but I don't remember. What prime example are you referring to? :)
> My personal favorite is:
> Matrix rows: 5 columns: 6 tabulate: #@ "for quickly setting up an example matrix :-)"

Why not simply
        Matrix rows: 5 columns: 6 tabulate: [:x :y | x @ y]

What's wrong with blocks?

>
> I find it rather confusing that the following does not work:
> #raisedTo: value: 2 value: 3. "works"
> #raisedTo:modulo: value: 2 value: 3 value: 4. "does not understand"
> This is an inconsistency.

Yes. The first case should also bail.

A symbol answering to #perform: is IMHO borderline.
A symbol's answer to #value should be itself.
A symbol should not answer to an #value: … #value:value:...


>
> @Eliot:
> > Can you give some examples of where you find you are needing to do this?
>
> Actually, I was trying something like this:
> SequenceableCollection >> #collectX: aBlock
> ^ aBlock numArgs = 2
> ifTrue: [self withIndexCollect: aBlock]
> ifFalse: [self collect: aBlock]].
>
> Now you can say:
> #(1 2 3) collectX: [:x | x squared].
> #(1 2 3) collectX: #squared.
> #(1 2 3) collectX: [:x :i | x + i].

That's hacky.

I would then rather use withIndexCollect. It does what it says on the tin.

> But the following does not work:
> #(1 2 3) collectX: #+.
>
> Maybe I would rather need something like #value:cull: for an optional second argument?
>

you can use cull:cull: anyways.

> In general, from the point of scripting convenience, I would love Symbol to support many more protocols of BlockClosure. What about:
> Symbol >> #asBlock
> ^ self numArgs caseOf: {
> [0] -> [[:rcvr | rcvr perform: self]].
> [1] -> [[:rcvr :arg | rcvr perform: self with: arg]].
> [2] -> [[:rcrv :arg1 :arg2 | rcrv perform: self with: arg1 with: arg2]].
> ... }
>
that's not too bad :D
but there is already perform:withArguments:, so this should be simpler?

Best regards
        -Tobias

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20191216/962dee4a/attachment.html>


More information about the Squeak-dev mailing list