4

I have a very simple Erlang program:

-module(test).
-export([start/0]).

Code = "Z00887". 
start() -> io:fwrite(Code).

And I have follows two errors:

c:/erl6.1/dev/test.erl:4: syntax error before: Code
c:/erl6.1/dev/test.erl:5: variable 'Code' is unbound

Could you please help me correctly using variables in my code.

5
  • 1
    Use the -define(Const, Replacement). macro, if you want to define a constant outside of a method erlang.org/doc/reference_manual/macros.html or do: start() -> Code = "Z00887", io:fwrite(Code).
    – Evan P
    Sep 10, 2014 at 16:37
  • 1
    possible duplicate of trouble when using anonymous functions inside erlang modules
    – legoscia
    Sep 10, 2014 at 16:41
  • Thank you very much! start() -> Code = "Z00887", io:fwrite(Code). is work!!! Sep 10, 2014 at 19:04
  • @legoscia Its not a duplicate, but it is a similar point of confusion for beginners who have no background at all in languages similar to Erlang (and the question code above is very close to another beginner point of confusion: use of io:fwrite/1).
    – zxq9
    Sep 10, 2014 at 21:31
  • A different, but closely related, question with solid answers is here: stackoverflow.com/questions/12719103/…
    – zxq9
    Sep 10, 2014 at 21:57

1 Answer 1

7

You are defining a variable that is global to the module, which is not allowed. Remember, "variables" in Erlang are really "symbols", so there is no concept of a "global" constant across all functions or processes. The closest thing to this in Erlang would be a macro defined in the module, but if a value is only needed in one place and you want to name it then this must be done within the function definition.

Also, do not use io:fwrite/1 or io:format/1. The problem is the possible inclusion of escape characters in the string you are passing. For example, this causes an error: Code = "Wee~!", io:format(Code). and it will not be caught by the compiler.

The most common thing to do is define a variable within the function:

-module(foo).
-export([start/0]).

start() ->
    Code = "Z00887",
    io:fwrite("~p~n", [Code]).

You could also just use the value directly:

-module(foo).
-export([start/0]).

start() ->
    io:fwrite("Z00887~n").

Or you could define a macro across the whole module:

-module(foo).
-export([start/0]).

-define(CODE, "Z00887").

start() ->
    io:fwrite("~p~n", [?CODE]).

Or you could even define a stub function that returns what you want:

-module(foo).
-export([start/0]).

start() ->
    io:fwrite("~p~n", [code()]).

code() -> "Z00887".

This last version is actually not as weird as it may seem at first. Very often when developing some code early on you will know you need a value somewhere that you will need to derive in some way, but don't want to worry about the details of it just yet. A stub function is an excellent way to hide the details of how you will do that in the future without writing macro code, variable definitions, etc. that you will have to remember to go back and change later. For example, the last example above will almost certainly change to something like this in the future:

-module(foo).
-export([start/0]).

start() ->
    io:fwrite("~p~n", [code()]).

code() ->
    {ok, Code} = some_init_module:get_code(),
    Code.

Keep this in mind. It makes Erlang almost as prototype-friendly as Guile or Scheme.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.