[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: macro like "my" in Perl

From: Lynn Winebarger
Subject: Re: macro like "my" in Perl
Date: Wed, 26 Jun 2002 09:44:52 -0500

On Tuesday 25 June 2002 21:25, Alex Shinn wrote:
> I guess that's the real complaint... nested lets indent the code too
> much, but for some straight but very long procedural algorithms it's
> less clear to make all the variable decls up front when their use is
> localized.
     If it's very procedural, why is it indented so far that nested lets will
indent the code "too much"?   And how is not indenting new scopes
going to clarify where "localized" definitions come from?  (Where 
"localized" is visual only, as the declaration extends to the end of
the enclosing block).
      Of course, he can do whatever he wants.
> You can write new-scope as a low-level macro that walks the code,
> turning my-define into set! and accumulating all the variable names used
> into one top-level let.  Something like
> (define-macro (new-scope . code)
>   (let ((vars (map cadr (filter (lambda (x) (eq? (car x) 'my-define))
>                                 code))))
>     `(let (,@(map (lambda (x) (list x #f)) vars))
>        ,@(map (lambda (x) (if (and (pair? x) (eq? (car x) 'my-define))
>                               (cons 'set! (cdr x))
>                               x))
>               code))))
> but make it recurse (this only works if all the my-define's are at the
> top level).

   If you must, why not call it "perl-scope" and introduce "my" as a syntax 
keyword (as is "(my x 'initial-value-here)").
   And I would suggest not fully recursing - you do (presumably) still want the
various let forms to introduce new scopes.  Recursing over begins and ifs 
should be sufficient.  Although perl has it's own rules for how my variables 
scope (like introducing them as the iteration variable in a loop), you might 
want to imitate those as well.  
   Also, what happens if the variable is used before declared?  Will that be an
error?  That _would_ require full recursion.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]