develooper Front page | perl.perl6.language | Postings from June 2005

Anonymous macros?

Thread Next
From:
Ingo Blechschmidt
Date:
June 13, 2005 08:27
Subject:
Anonymous macros?
Message ID:
d8k896$3er$1@sea.gmane.org
Hi,

just checking: Are anonymous macros allowed?

  my $macro = macro ($x) { "100$x" };
  say $macro(3); # 1003

Of course, anonymous macros can't be called at compile-time, like normal
macros:
  my $macro = rand < 0.5
    ?? macro ($x) { "100$x" }
    :: macro ($x) { "200$x" };
  say $macro(3); # 1003 or 2003?

But they're still useful if I want to work with $macro references and
store them later, at compile-time, in a &-var:
  my &macro = BEGIN {
    rand < 0.5
      ?? macro ($x) { "100$x" }
      :: macro ($x) { "200$x" };
  };
  say macro 3; # reduced at compile-time to either 1003 or 2003


--Ingo

-- 
Linux, the choice of a GNU | The computer revolution is over. The
generation on a dual AMD   | computers won. -- Eduard Bloch <edi@gmx.de>
Athlon!                    | 


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About