Scope of locals and parameters 
Author Message
 Scope of locals and parameters

Dear Smalltalk community

I've got a question about scopes of variables in Smalltalk.
Until now I thought that locals and parameters are local to
the method they are defined in. Well, but when I look at
blocks and their evaluation, it seems a bit different:

Say, a method foo has these lines:
..........
s := 'bar'.
true ifTrue: [^s].
..........

What happens during execution?
Message *ifTrue:* is sent to True with parameter [^s], (a BlockClosure).

*ifTrue:* is defined in True as:    
ifTrue: alternativeBlock
        ^alternativeBlock value

So, the BlockClosure [^s] if sent the message *value*.

*value* is defined in BlockClosure as:
value
        <primitive: 501>

It seems to find the value of *s*, this primitive has to go up
the chain of activation records or whatever to find the environment
that contains *s* -> 'bar'. Or how else is this done? Or does the
Smalltalk compiler do some tricks here with in-line compiling - I'd
like to know how?

Any comments are appreciated

Stephan



Mon, 05 Feb 1996 20:35:19 GMT  
 Scope of locals and parameters
|> I've got a question about scopes of variables in Smalltalk.
|> Until now I thought that locals and parameters are local to
|> the method they are defined in. Well, but when I look at
|> blocks and their evaluation, it seems a bit different:
|>
|> Say, a method foo has these lines:
|> ..........
|> s := 'bar'.
|> true ifTrue: [^s].
|> ..........
|>
|> What happens during execution?

In some versions of Smalltalk, blocks don't really have their own scope.
They just evaluate in the scope of the method they are defined in.  In
such systems, block arguments are really just more temps in the defining
method.  For example:

foo
        | a b |
        'hello' do: [:char | ]

will actually have three temp vars.  As far as I know, Smalltalk/V and
old Smalltalk-80 systems use this approach.

In other versions of Smalltalk, blocks have their own scope and the variable
lookup searches up from there, just as in Pascal.  ParcPlace Smalltalk-80
has been using this approach for some time.
--
   ,__o                                                  ,__o

(*)/'(*)                                              (*)/'(*)



Mon, 05 Feb 1996 22:57:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Scope of locals and parameters

2. Local variable scope

3. load and local variable scope (and constants too)

4. Scope of local defines?

5. about the the scope of a local variable

6. Why is $_ local to the current scope?

7. Withdrawn SRFI 24: Define-syntax in local lexical scopes

8. SRFI 24: Define-syntax in local lexical scopes

9. Relate Tcl_SetVar variables to local scope in Tcl_Eval?

10. Scope of local and global variables

11. Question re local bindings in nested scopes

12. no re-binding of nested-scope locals, no re-binding in lambda, no,,, {was Re: lambda)

 

 
Powered by phpBB® Forum Software