4 ms·
Macropy: Syntactic Macros for Python
- nubela 7y agoOther than `@case`, are there other examples of macropy macros?? @case means nothing to me.
- carver 7y agoTail call optimization is appealing. Sometimes recursion is the cleanest implementation, but call depth is not known up front.
- lihaoyi 7y agoThe page has links to a dozen different examples, with code snippets and discussion of the motivations and potential use cases for each
- sitkack 7y agoTotally 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?
- anentropic 7y agoThis project is great Would love to see a new release on PyPI that works with Python > 3.4 ;) (last release seems to be "1.1b2" in 2018...)
- memco 7y agoI’m having a hard time understanding what advantages @case has over data classes.
- lihaoyi 7y agoThe main reason @case is an example macro is that data classes did not exist when this project was conceived
- deleted 7y ago[deleted]
- dhash 7y agoLihaoyi mostly authors scala libraries. I switched from python to scala and found many of the things that I love about scala ported to python with this library. Case classes (pretty .toString, structural equality, .copy, and destructuring), Scala 3-esque enums, f-string interpolations before python added them, pattern matching, and _ based lambdas are the highlights for me.
- lihaoyi 7y agoAuthor here, funny to see this up here again after so many years. Happy to answer questions
- cjauvin 7y agoI 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 7y agoAs 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
- riazrizvi 7y agoVery cool. Was this inspired by Hy?
- lihaoyi 7y agoPretty sure we MacroPy predates Hy by about a year
- tekacs 7y agoNot 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.
- kbob 7y agoMacropy 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.