[REBOL] Re: local vars in functions
From: rishioswal:yaho:o at: 24-Apr-2002 12:18
--- Joel Neely <[joel--neely--fedex--com]> wrote:
> Hi, Rishi,
>
> My uninformed $0.02...
>
> (Note that I try to make an alternative suggestion
> at the
> end of my discussion. I'd be interested in feedback
> on
> that idea!)
>
> The main root problem I have with your suggestion is
> that it
> seems to make an assumption about REBOL that I do
> not believe
> to be true -- that there's some sort of conventional
> "compile"
> step that preprocesses the entire text of a piece of
> code
> before generating some internal representation.
>
> For why I think this, see below. Please let me know
> if I
> have misunderstood your meaning...
>
> Rishi Oswal wrote:
> >
> ...
> > What I would like to see is another shortcut to
> > creating local variables in any context (function,
> > innerfunction, loop). The obvious way I see of
> doing
> > this is as follows:
> >
> > myfunc: func [][
> > localvar:: $25
> > myinnerfunc: func [][
> > innerlocal:: $10
> > print localvar ; prints $25
> > ]
> > print innerlocal; error!
> > ]
> > print localvar ; error!
> >
>
> Based on your description, should I infer that you
> would like
> the above to be equivalent to?
>
> myfunc: func [/local localvar myinnerfunc] [
> localvar: $25
> myinnerfunc: func [/local innerlocal] [
> innerlocal: $10
> print localvar
> ]
> print innerlocal ; error
> ]
> print localvar ; error
>
hi joel. the code i meant to write is shown below. I
should have made myinnerfunc end with "::". This code
below should be exactly identical to the code you
wrote above. Looks like you have thought more deeply
into the problem than I have and I will take a look at
it tonight - rough schedule today :)
myfunc: func [] [
localvar:: $25
myinnerfunc:: func [] [
innerlocal:: $10
print localvar
]
print innerlocal ; error
]
print localvar ; error
Note that I assumed you meant for MYINNERFUNC to be
> local to
> MYFUNC even though you didn't use the double colon
> (because
> of the way you named it).
>
> Do you intend the above meaning or do you intend an
> alternative meaning, such as:
>
> myfunc: func [] [
> use [localvar] [
> localvar: $25
> use [myinnerfunc] [
> myinnerfunc: func [] [
> use [innerlocal] [
> innerlocal: $10
> print localvar
> ]
> ]
> print innerlocal ; error
> ]
> ]
> ]
> print localvar ; error
>
> Alternately, I could pose it as two questions:
>
> 1) What context do you think should contain the
> double-colon
> variables?
>
> 1a) The context of the smallest enclosing
> function
> definition?
yes.
> 1b) A new context whose scope is described
> below?
>
> 2) How far should the scope of that context extend
> (lexically
> I mean)?
>
> 2a) From just before the double-colon
> expression to the end
> of the enclosing block?
>
> 2b) From the beginning of the enclosing block
> to the end of
> the enclosing block?
>
> 2c) Just the following expression?
>
> >
> > using the "::" for local var will make it more
> > convienient to create local vars (which i use all
> the
> > time over global vars). In addition, it will help
> > prevent some errors of accidental global var
> creation
> > because it is now easy to spot a local var.
> >
>
> Pardon me, but I must disagree. The typographic
> differences
> among the following expressions
>
> foo: 10
> bletch:: "this is a test"
> somelongervariablename: foo + length? bletch
> shortername:: somelongervariablename + 5
> * (thing1: square-root 75) - (thing2::
> now/time/second)
> lastone: shortername + 7
>
> do *not* IMHO make the local vs. non-local names
> stand out
> all that much.
>
> >
> > Best of
> > all, this type of shortcut would not break
> anything in
> > rebol. You could even use this in a loop:
> >
> > for count 1 10 1 [
> > localvar:: "hello"
> > ]
> >
>
> Based on this example, I assume you *must* mean (1b)
> is the
> context you want, but I'm still unclear what the
> answer to
> (2) would be.
>
> I'm also not clear on what semantics you intend e.g.
> for the
> following case
>
> myfunc1: func [s [string!]] [
> a:: 1
> foreach char s [
> a:: a + 1
> if all [#"A" <= char char <= #"Z"] [
> a: a + 1
> ]
> if all [#"a" <= char char <= #"z"] [
> a:: a + 1
> ]
> if all [#"0" <= char char <= #"9"] [
> a: a + 1
> ]
> ]
> a
> ]
>
> >
> > Using the "::" shortcut in a global context would
> be
> > the same as using a ":".
> >
>
> So "::" behaves differently depending on "context"
> (I
> assume you mean lexical setting, not REBOL
> "context"...)
> and on which time it is being evaluated???
>
> How does it know? (See below.)
>
> That's not the same as defining e.g.
>
> print flarp:: square-root length? "Yowp!"
>
> as meaning the same as
>
> use [flarp] [
> print flarp: square-root length? "Yowp!"
> ]
>
> Remember that LOADing an expression implicitly wraps
> it in
>
=== message truncated ===