I am going through Erlang code.
tes_lib:check_operational(Config)
The above code is present in a module called Sample.erl
.
I am new to this language. My question is that I cannot see any include statement for the module tes_lib
in Sample.erl
. So, how come Sample.erl
is able to call the function check_operational
using tes_lib
module?
I thought that it should be like Java, where we first import the class and then call the function.
In Erlang, you don't need to "import" modules in order to be able to call them. A call like tes_lib:check_operational(Config)
will be resolved at runtime. If the tes_lib
module hasn't been loaded yet, the code server will look for it in the load path, and if the module can't be found, the call will fail with an undef
error.
There is an -import
directive in Erlang, but it's usually considered bad style to use it. You could write:
-import(tes_lib, [check_operational/1]).
and then call check_operational
as if it were a local function, without specifying the module name. Those function calls will be replaced by fully qualified calls at compile time.
From the Erlang Programming Rules:
Don't use -import
, using it makes the code harder to read since you cannot directly see in what module a function is defined. Use exref
(Cross Reference Tool) to find module dependencies.