Blocks and self?

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

Blocks and self?

Günther Schmidt
Hi,

if blocks are objects is there a way to refer within a block to "self" the block?
I don't have a use scenario for it at the moment, it's merely an academic question.

Günther


Reply | Threaded
Open this post in threaded view
|

Re: Blocks and self?

Christopher J. Demers
"Günther Schmidt" <[hidden email]> wrote in message
news:423b8fca$[hidden email]...

> if blocks are objects is there a way to refer within a block to "self" the
> block?
> I don't have a use scenario for it at the moment, it's merely an academic
> question.

I think the code bellow may illustrate what you are asking for.  Copy the
text bellow into a workspace and evaluate it all at once via Ctrl-D.
===========
| theBlock |
theBlock := [:arg |
    arg < 100 ifTrue: [theBlock value: arg + 1] ifFalse: [arg]].
theBlock value: 1.
===========

In the above example the block can refer to itself since we have assigned it
to a variable within scope of the block.  I suspect that there may be a more
"magical" way to get a reference to a block within itself using contexts and
such, though I would avoid such an approach in a real program.

Chris


Reply | Threaded
Open this post in threaded view
|

Re: Blocks and self?

Günther Schmidt
Christopher J. Demers schrieb:

> "Günther Schmidt" <[hidden email]> wrote in message
> news:423b8fca$[hidden email]...
>
>
>>if blocks are objects is there a way to refer within a block to "self" the
>>block?
>>I don't have a use scenario for it at the moment, it's merely an academic
>>question.
>
>
> I think the code bellow may illustrate what you are asking for.  Copy the
> text bellow into a workspace and evaluate it all at once via Ctrl-D.
> ===========
> | theBlock |
> theBlock := [:arg |
>     arg < 100 ifTrue: [theBlock value: arg + 1] ifFalse: [arg]].
> theBlock value: 1.
> ===========
>
> In the above example the block can refer to itself since we have assigned it
> to a variable within scope of the block.  I suspect that there may be a more
> "magical" way to get a reference to a block within itself using contexts and
> such, though I would avoid such an approach in a real program.
>
> Chris

Hi Chris,

thanks, I actually was already aware of that particular solution. As I said I don't even see a use scenary for it, it was merely an academic question.


You wouldn't necessarily be working on a RoboCode sortof game right now, eh?

Günther


Reply | Threaded
Open this post in threaded view
|

Re: Blocks and self?

Chris Uppal-3
Günther Schmidt wrote:

> As I
> said I don't even see a use scenary for it, it was merely an academic
> question.

Since it's only academic, I think a class side method on BlockClosure like:

    =========
    current
         ^ Processor activeProcess topFrame sender block.
    =========

will give you access the object in question.  E.g.

    [:x | BlockClosure current argumentCount] value: nil.
        "--> 1"

A more robust (!) implementation would probably search the stack for the
topmost BlockFrame rather than just assuming it was called from inside the body
of an executing BlockClosure.

    -- chris


Reply | Threaded
Open this post in threaded view
|

Re: Blocks and self?

Christopher J. Demers
In reply to this post by Günther Schmidt
"Günther Schmidt" <[hidden email]> wrote in message
news:[hidden email]...
> You wouldn't necessarily be working on a RoboCode sortof game right now,
> eh?

No, I am not currently working on any RoboCode stuff, though it sounds
interesting.  However you can see the game I did develop here
http://www.cjd77.com/smalltalk/CJDSimpleGame.htm .  It is nothing fancy, but
it was an entertaining little distraction.

Chris


Reply | Threaded
Open this post in threaded view
|

Re: Blocks and self?

Eliot Miranda
In reply to this post by Günther Schmidt
Günther Schmidt wrote:

> Hi,
>
> if blocks are objects is there a way to refer within a block to "self"
> the block?

thisContext.  Then, depending on dialect you either have the block
(blue-book non-reentrant blocks) or its activation (closures).  If the
latter in VisualWorks then
        thisContext receiver
gets the BlockClosure.  Other dialects will have other implementations.
    One good way to find this out is to do e.g.

| outer |
[| tc |
   "make sure this is a full block with a reference to its outer context"
  outer class notNil ifTrue: [^'eek!'].
  tc := thisContext.
  self halt] value

and then start digging around from the tc temp in the debugger.


--
_______________,,,^..^,,,____________________________
Eliot Miranda              Smalltalk - Scene not herd


Reply | Threaded
Open this post in threaded view
|

Re: Blocks and self?

Günther Schmidt
Hi Eliot,

I found your recent responses to posts in this group very interesting and insightful, I'm just courious, I don't think I've seen your name previously (I've only been on this list for about 6 months though).

Well in short I wonder if you would like to reveal a bit more about your background?

Günther


Reply | Threaded
Open this post in threaded view
|

Re: Blocks and self?

Eliot Miranda
Günther Schmidt wrote:
> Hi Eliot,
>
> I found your recent responses to posts in this group very interesting
> and insightful, I'm just courious, I don't think I've seen your name
> previously (I've only been on this list for about 6 months though).

thanks. you're welcome.

> Well in short I wonder if you would like to reveal a bit more about your
> background?

http://wiki.cs.uiuc.edu/VisualWorks/Eliot+Miranda
--
_______________,,,^..^,,,____________________________
Eliot Miranda              Smalltalk - Scene not herd