Hacker News new | past | comments | ask | show | jobs | submit login

"Erlang does not provide macros"

I never wrote any myself, but what about parse transformations? Aren't they essentially non-hygienic macros?




Sorry, I could have expressed myself better.

Erlang does not provide lisp style macros. They provide macros, but they look more like templates and are limited. For example, a couple days ago I wanted to write this macro:

    -define(line(Opts), case lists:keyfind(line, 1, Opts) of
      { line, Value } -> Value;
      false -> 0
    end).
I can call this macro as:

    ?line(SomeOpts)
Basically, the compiler will replace ?line by the template defined above. You cannot manipulate the AST via such macros and they contain severe limitations. In particular, the example above just works if you use it once per function. Since variables are not hygienic, after you use ?line once, the variable Value is going to be bound and it will be considered unsafe (because it is defined in just one of the branches) and your code will fail to compile if you use it again.

In case you want to manipulate the AST, you can indeed use parse transforms. But it requires a considerable amount of work to get simple stuff done and iirc you can just use one per module.


Just minor nitpick: "lisp style macros" are not necessarily hygienic either, which is why you need (gensym). Not in Scheme, though.

"iirc you can just use one per module"

I happen to use erlando[1] often (it's fun!) and I use both do and cut transforms all the time. So no, there is no limit to how many parse transforms you can use in one module.

It's true, however, that writing them is a pain, which is why I never bothered to write one (just take a look at how the 'do' is implemented) - but I just accepted this as inevitable cost of not using s-exps as a language syntax. I'll take a look at how Elixir does it, I'd be very positively surprised if it manages to get real macros convenient in non-s-exp language :)

[1] https://github.com/rabbitmq/erlando




Consider applying for YC's Spring batch! Applications are open till Feb 11.

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

Search: