Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I've read and heard the commonly echoed sentiments about Macros being used to build a language for your application. I'm a bit confused in how this is philosophically different than just writing domain specific classes and functions? Can someone point to an example where the syntax allowed by Ruby or JS produced obfuscated code and a LISP macro produced something really simple comparatively?


It's the difference between introducing new nouns and verbs, and introducing new grammar.

With new classes, I add new nouns (Circle, for example). With functions, I add new verbs (move, for example). And I can add adjectives and adverbs (attributes and arguments). This lets me build a "new language" (for handling shapes, in my example).

But macros let you go much further. They let you change the syntax, so that instead of saying "shape.move(newCoords)", you can say "move shape newCoords", for example, even though that was not legal syntax in the language you're writing in. That's where it really becomes a new language - not just new words.

But if you've got "good enough" syntax in the language you're writing in, you might not see that as particularly useful. That is, the less impoverished your out-of-the-box implementation language syntax is, the less you need macros. (Most of the examples say "it's awkward to write this; I can write a macro to make it easier". Well, that's great and all, but awkwardness of writing something that I don't write very often in the first place does not make me want to run out and adopt a macro language.)


My personal favorite is the series library. I wrote a bit about it here: https://news.ycombinator.com/item?id=9078155


Statically compiled regular expressions. Efficient pattern matching implementations. Generating a boilerplate for an ORM. Embedding languages with arbitrarily complex syntax. There are hundreds of things that you simply cannot do with functions but are trivial with macros.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: