
Macropy: Syntactic Macros for Python - todsacerdoti
https://macropy3.readthedocs.io/en/latest/
======
kbob
Macropy transforms Python's AST. So it doesn't allow anything that Python
can't parse.

I spent some time earlier this year looking at tapping into the Python
compilation process before the AST is generated, in order to allow statements
that are not legal Python syntax.

It's definitely possible using the `tokenize` module, but doing it right was
turning into more work than the quick hack I wanted.

------
lihaoyi
Author here, funny to see this up here again after so many years. Happy to
answer questions

~~~
cjauvin
I don't know much about macros and it revived my interest for this topic,
thanks. I wonder if it could be used to embed SQL statements directly in
Python code? (not sure it would be a good idea, but it would make for an
interesting experiment maybe).

~~~
lihaoyi
As implemented, we do not change the superficial python syntax. That has a
number of advantages around compatibility, but it means embedding raw SQL
outside string literals isn’t possible. We could do things like
validating/transforming SQL strings at compile time, like what we do with
string interpolators or pyxl strings

------
nubela
Other than `@case`, are there other examples of macropy macros?? @case means
nothing to me.

~~~
lihaoyi
The page has links to a dozen different examples, with code snippets and
discussion of the motivations and potential use cases for each

~~~
sitkack
Totally off topic, but I was wondering if you have looked at Coconut, a kind
of ML Lite that compiles to Python? And if so, what do you think?

[http://coconut-lang.org/](http://coconut-lang.org/)

Are you still mainly programming in Scala? Do you see yourself programming in
your current language in 5 years?

------
riazrizvi
Very cool. Was this inspired by Hy?

~~~
lihaoyi
Pretty sure we MacroPy predates Hy by about a year

~~~
tekacs
Not unless there was an older repo that is now gone:

[https://github.com/lihaoyi/macropy/graphs/contributors](https://github.com/lihaoyi/macropy/graphs/contributors)

[https://github.com/hylang/hy/graphs/contributors](https://github.com/hylang/hy/graphs/contributors)

It looks like macropy started around 6 months after Hy, around a time when Hy
had multiple contributors going.

Perhaps Hy hadn’t really been heard about yet.

