[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]
syntax for macros / function composition at compile time
Hi list!
I need some enlightened help with a matter of language syntax design.
I have been toying recently with a Python feature called a decorator
and adapted it to Perl in the CPAN module Python::Decorator. Note that
this decorator is not related to the design pattern of the same name.
A decorator is simply syntax sugar to apply some functions to a
subroutine at compile time. It acts very much like LISP macros, or
compile-time function composition in functional languages, and it uses
higher order functions ($cool++ :).
Python::Decorator lets your write:
@memoize
@validate_args("int")
sub incr {
return $_[0]+1;
}
in order to, say, add a memoizing ability to incr() and a layer of
argument validation that checks that incr()'s only argument is an
integer.
Anyway, the interesting fact here is that incr() ends up being
replaced at compile time by the result of:
memoize(validate_args("int")->(sub { return $_[0]+1; }))
Both memoize() and validate_args() are higher order functions (take a
function and return one). Python::Decorator works by implementing a
source filter that uses PPI to modify the source. Quite experimental
but works most of the time :)
For more info about decorators, check out Python::Decorator on CPAN or
read http://www.artima.com/weblogs/viewpost.jsp?thread=240808 and
http://www.artima.com/weblogs/viewpost.jsp?thread=240845.
My problems are the following:
1. WHAT SYNTAX SHOULD DECORATORS HAVE IN PERL?
2. WHAT SHOULD THE DECORATOR MODULE BE CALLED?
About the module's name first: the name "Python::Decorator" works for
a proof of concept but obviously not for a Perl feature :) So what?
Naming it "macro" would be pretentious because it is not a full
implementation of macros. May be "sub::macro" since it does emulate
macros applied to subroutines... Or should we have a functionally
inspired name, like "sub::compose"?
Then, about the bigger issue of the syntax. Here are a few things to
meditate on:
- should it be compilable Perl? I would say no so that forgetting to
"use sub::macro;" (or whatever its name) would make the source not
compilable instead of silently compiling it into something unexpected.
- should we keep the Python syntax of "@subname", with the '@'?
Probably not, it looks too much like a list variable...
- what about putting the decorators inside the sub declaration, something like:
sub memoize:validate_args("int"):incr {
return $_[0]+1;
}
humm... messy. The sub declaration line would quickly become too long.
There should probably be one decorator per line, as in Python, for the
sake or readability. But we could also accept multi line sub
declarations:
sub memoize:
debug:
validate_args("int"):
incr {
return $_[0]+1;
}
I like that last one.
Well, ideas? suggestions?
Syntax examples would be highly appreciated!
Cheers,
/Erwan
- Follow-Ups from:
-
Michael G Schwern <schwern@pobox.com>
"David Nicol" <davidnicol@gmail.com>
[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]