Search code examples
erlangerlang-shell

error in program while comparing two string in macro in erlang


I have written the macro to compare two string as follow

-module(helloworld). 
-export([start/0]). 
-define(macro1(X,Y),{if X == Y -> "True"; true ->"False" end.}). 

start() ->
   io:fwrite("~w",[?macro1("str","str")]).

getting the error as follow:

Compiling the source code....
$erlc helloworld.erl 2>&1
helloworld.erl:6: syntax error before: '.'
helloworld.erl:2: function start/0 undefined

Solution

  • Macro are not like function definition. The pre processor simply does string replacement. so in your case you have to remove the dot in the curly bracket:

    -define(macro1(X,Y),{if X == Y -> "True"; true ->"False" end}).