Hi,

The first part could be done, but with limitations.

- you won't be able to value it with anything but variables names

- those variables cannot have the same name as a method of BlockClosure

- I don't think this can be implemented by default in Pharo, as it is kinda "dangerous"

- might be unreliable, i don't know how stable Context is. ( For my own experiments, I have some problem with P7, but my stuff work fine on P8)


You'd have to implement something like:

BlockClosure >> DoesNotUnderstand: aMessage
    valueOfSelector := self outerContext at: aMessage selector ifAbsent:[ ^ super doesNotUnderstand "The variable isn't found in the context, so it's a real DNU "].

    self valueWithArguments: valueOfSelector asArray "allows us to have several arguments"


So if you did:

aVariable := 3.

[:x | x + 3] aVariable.


The block would say it didn't understand the message aVariable, and you'd perform it in the doesNotUnderstand hook you just override.

I guess it'd be something similar for the second part of your question, but i'm pretty sure it becomes unreadable rather quickly ˆˆ


PS: I didn't test this, but it's something in that spirit.

the context access might not be outerContext.

Pierre.


On 23/10/2019 09:22, main wrote:
Hello fellow Pharoians (?) from a lonely Swede.

I just found out about Pharo (and Smalltalk) two days ago and I'm already
loving it :)
However, there is a behavior I would like to change, or be enlightened about
how it could be done.

As I understand it (bear with me) BlockClosure from Kernel-Methods does not
understand (by default?) how to respond to an "anonymous object" (no message
name).

Is there any way this could be implemented? I'll post an example soon
(I currently use both Pharo 7 and 8)

If I write the following:

[:x | x + 1] value: 3

and evaluate it, I get the expected result, which is 4.

That's nice.

What I would really like is to be able to just send 3 to BlockClosure and
make it repond as above.

Like this:

[:x | x + 1] 3

or like this:

3 [:x | x + 1]

Would this be possible?

-----------------------

Also as a bonus, would it be possible to use this for "function
composition", "Block composition" or similar?

Example of composition:

[ :f :g | [ :x | f value: (g value: x) ] ]

I could use this construct like this for example:

(([ :f :g | [ :x | f value: (g value: x) ] ])
value: [:x | x + 1]
value: [:x | x - 1])
value: 5

But… It's a bit wordy.

What I would like to be able to do (essentialy remove "value:"):

(([ :f :g | [ :x | f (g x) ] ])
[:x | x + 1]
[:x | x - 1])
5.

Or in one line:
(([ :f :g | [ :x | f (g x) ] ]) [:x | x + 1] [:x | x - 1]) 5.

While this might seem obscure, I would really find it useful.

Can it be done?

Thanks in advance



--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html


Reply via email to