Converting blocks to methods

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

Converting blocks to methods

Martin Beck-3
Hi there,

has anyone tried to accomplish something like

MyNewClass addMethod:#doSomethingWith: with: [ :arg |
        Transcript show: arg].

MyNewClass new doSomethingWith: 'Hello World'.

in Squeak? This would mean, to convert a block into a method, thus
changing the hole semantics. Outer temporaries or other variables are of
course forbidden. Literals have to be fetched out of the defining method
and brought into a new compiled method and so on. Or perhaps there are
other possibilities? Any ideas?

Regards,
Martin

Reply | Threaded
Open this post in threaded view
|

Re: Converting blocks to methods

Randal L. Schwartz
>>>>> "Martin" == Martin Beck <[hidden email]> writes:

Martin> has anyone tried to accomplish something like

Martin> MyNewClass addMethod:#doSomethingWith: with: [ :arg |
Martin> Transcript show: arg].

Martin> MyNewClass new doSomethingWith: 'Hello World'.

Martin> in Squeak? This would mean, to convert a block into a method, thus
Martin> changing the hole semantics. Outer temporaries or other variables are
Martin> of course forbidden. Literals have to be fetched out of the defining
Martin> method and brought into a new compiled method and so on. Or perhaps
Martin> there are other possibilities? Any ideas?

You could invoke the compiler on a decompiled version of the block.  Not the
fastest solution, but probably the easiest. :)

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[hidden email]> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

Reply | Threaded
Open this post in threaded view
|

Re: Converting blocks to methods

Klaus D. Witzel
In reply to this post by Martin Beck-3
This is of course possible in Smalltalk, alt-G the following:

!Object class methodsFor: 'Martin-Methods' stamp: 'kwl 1/19/2008 18:41'!
addMethod: aSelector with: aBlock
        "Object addMethod: #doSomethingWith: with: [:argv | Transcript cr; show:  
argv]"
        "Object new doSomethingWith: 'Hello World'"
        self compile: aSelector, ' argv
                #MartinBlock value: argv ' classified: #'Martin-Methods'.
        ^ (self compiledMethodAt: aSelector) literalAt: 1 put: aBlock; yourself! !

and doIt the two expressions in the comment lines.

IMHO it's rather a matter of taste to spend one new compiled method for  
invoking one block.

Also, the scope of the block is related to creation time which may hold  
its objects far longer than is wanted for many cases. But it's Smalltalk  
and so that can be changed as well (not shown in the above).

Enjoy!

/Klaus

On Sat, 19 Jan 2008 17:42:28 +0100, Martin Beck wrote:

> Hi there,
>
> has anyone tried to accomplish something like
>
> MyNewClass addMethod:#doSomethingWith: with: [ :arg |
> Transcript show: arg].
>
> MyNewClass new doSomethingWith: 'Hello World'.
>
> in Squeak? This would mean, to convert a block into a method, thus
> changing the hole semantics. Outer temporaries or other variables are of
> course forbidden. Literals have to be fetched out of the defining method
> and brought into a new compiled method and so on. Or perhaps there are
> other possibilities? Any ideas?
>
> Regards,
> Martin
>
>