[erlang-questions] A small question about macros

Richard A. O'Keefe ok@REDACTED
Thu May 16 07:40:38 CEST 2013


On 16/05/2013, at 4:04 PM, Martin Hedberg wrote:
> I read about how to make macros in the language. However I wonder how I do if I want 
> to have blank spaces in my commands? So I just can write, for example: "repeat 5 times". 

What do you mean by "commands"?  I'm not sure that Erlang has anything like
that.

Erlang macros were introduced as a quick and dirty solution to some real
problems (giving names to numbers so they can be used in patterns, abstracting
patterns the way functions abstract expressions).  They are not meant as
general purpose language extension mechanism.

Nothing other than good taste and good sense will stop you doing

-define(repeat,     loop(().
-define(times,  ),  fun () ->).
-define(end_repeat, end)).

loop(N, F)
  when is_integer(N), N > 0 ->
    F(),
    loop(N-1, F);
loop(0, _) ->
    ok.

foo(N) ->
    ?repeat N ?times
        io:fwrite('Going against the grain of a language is not helpful.~n')
    ?end_repeat.

But please don't do that if you want me to read your code.

> Hope you don't see me as a heretic if I want to HyperTalk-ificat Erlang a little bit.

Erlang is a _practice_, not a religion.  I don't see you as a heretic,
more as an isolationist.




More information about the erlang-questions mailing list