Hello all!
I found MetaPython a couple of weeks ago, but just had a chance to look more into it last night and this morning. So far I like it's direction!
The namedtuple and point examples shows us that we can expand a simple definition into a more complicated abstraction. In all reality we could do this with exec, and that's exactly the way this is done in the collections library for namedtuple. I of course like the MetaPython version better, but wonder where the line currently is drawn.
Macros in homoiconic languages such as Lisps allow you to do a variety of different things:
1. Resource management (e.g. with-open-file)
In Python, the with-statement makes this sort of unnecessary of course.
2. Injection of behavior (e.g. memoization, debug printing)
For memoization, a simple decorator is more than sufficient, and for debug printing, the cremove.py file in the tests shows how we could write a macro which expanded to an if-statement, enforcing laziness through quoting.
3. Control-flow and looping constructs (e.g. unless, aif, etc)
We certainly can't do this in Python, and I don't see a way to do this (yet) in MetaPython--please correct me if I'm wrong. Ideally, we'd be able to *extend* the syntax of Python to fit our needs.
I'd love to be able to do something like:
defsyntax aif(cond:conditional_expression, ?:as, var:variable, ?::, true:stmt_list, ?:else, ?::, false:stmt_list=?{pass}):
var = cond
if var:
true
else:
false
or
defsyntax unless(cond:conditional_expression, ?::, true:stmt_list):
if not cond:
true
Where I could use them like:
def something(x):
$aif someregex.match(x) as matchobj:
return matchobj.group(1)
$else:
$unless len(x):
raise ValueError("failed because string length was 0")
Obviously contrived, (and not conforming to the conventions already established in MetaPython) but the point is that ?: in the argument list indicated an actual token, and we named non-terminals from python's parser as placeholders for the code we wanted to match.
As I understand it, I'd currently have to do unless(len(x), functioncall()), to get the desired semantics for unless, but maybe I'm wrong?
4. Alternate binding constructs (e.g. let, or match)
Wouldn't it be awesome if we could add pattern matching to Python in a way similar to OCaml's match statement? Wouldn't it be awesome if we could create alternate binding constructs, which let us destructure tuples and other nested structures more appropriately?
x, y, z = (1, 2, 3,) # now
x, {y['a'], z['b']} = (1, {'a': 2, 'b': 3},)
which would of course bind x = 1, y = 2, z = 3 in both cases.
5. Lazy evaluation
We seem to be able to achieve this now (see cif)
6. DSLs
Everyone has different definitions of DSLs. namedtuple could be considered part of a DSL, as can the example of has_attributes in the examples/ directory. Surely, better syntactic abstraction would lead to finer DSLs.
I don't see how anyone could disagree with having power like this in MetaPython, but my question is: how feasible is it to get there, is this stuff already the roadmap, and can I already do some of this that I just haven't discovered yet?
Thanks!
Andrew
--