Vombat
Vombat

Reputation: 1166

Opening a module in Erlang

Is there a way to open a module in Erlang and then call its functions without using module name prefix? Like opening an ML structure!

Upvotes: 1

Views: 777

Answers (3)

rvirding
rvirding

Reputation: 20916

No, you can't! The methods given by @johlo and @stemm are just work-arounds which allow you to not explicitly write the module name but that is all. The -import(...) declaration is a misnomer and doesn't do what you would expect.

Given Erlang's very dynamic handling of code it would be practically meaningless as well. There is no guarantee that at run-time you have the same "other" module as you had at compile-time, or if it is there at all. All code handling, compiling/loading/purging/reloading/etc. , is done on a module basis and there are no inter-module dependencies or optimisations.

Upvotes: 5

stemm
stemm

Reputation: 6040

Instead of import you can use defining:

-define(SIN(X), math:sin(X)).

my_func(X) -> ?SIN(X).

Upvotes: 1

johlo
johlo

Reputation: 5500

You can use

-import(my_module, [foo/1,bar/2]).

to import individual functions (in my example foo/1 and bar/2) from another module (my_module), see the modules documentation . There is no way of importing all functions from a module, they have to be explicitly listed.

Also see In Erlang how can I import all functions from a module? for an explanation why you shouldn't import functions!

Upvotes: 5

Related Questions