[erlang-questions] import/1

David Mercer <>
Fri Jun 12 17:45:12 CEST 2009


The way I handle this is I put a module's interface in an included HRL, and
then include it in both the module exporting the interface and any module
wanting to import the entire interface.  E.g., the exporting module would
include these lines:

	-define(export_parsec_pos_interface, ).
	-include("parsec_pos.hrl").

And any importing module would include these lines:

	-define(import_parsec_pos_interface, ).
	-include_lib("parsec/include/parsec_pos.hrl").

What does this HRL look like?  Something like this:

	%% Usage:
	%%
	%% If you just want type definitions:
	%% 	-include_lib("parsec/include/parsec_pos.hrl").
	%% or:
	%% 	-include("parsec_pos.hrl").
	%%
	%% If you also want to import the entire parsec_pos interface:
	%% 	-define(import_parsec_pos_interface, ).
	%% 	-include("parsec_pos.hrl").
	%%
	%% If you want to export the parsec_pos interface (e.g., in
parsec_pos.erl):
	%% 	-define(export_parsec_pos_interface, ).
	%% 	-include("parsec_pos.hrl").


	-ifndef(parsec_pos_interface).
		-define(parsec_pos_interface,
			  sourceLine/1
			, sourceColumn/1
			. . .
			).
	-endif.

	-ifdef(export_parsec_pos_interface).
		-ifndef(parsec_pos_interface_exported).
			-define(parsec_pos_interface_exported, ).
			-export([?parsec_pos_interface]).
		-endif.
	-endif.

	-ifdef(import_parsec_pos_interface).
		-ifndef(parsec_pos_interface_imported).
			-define(parsec_pos_interface_imported, ).
			-import(parsec_pos, [?parsec_pos_interface]).
		-endif.
	-endif.


	-ifndef(parsec_pos_types_defined).
		-define(parsec_pos_types_defined, ).

		-type source_name() :: string().

		-type line() :: integer().

		-type column() :: integer().

		-type source_pos() :: {source_pos, source_name() |
undefined, line(), column()}.

	-endif.


This approach also allows me to have HRL's that import multiple interfaces.
For example:

	-ifdef(import_parsec_interface).
		-ifndef(parsec_interface_imported).
			-define(parsec_interface_imported, ).
			-define(import_parsec_prim_interface, ).
			-define(import_parsec_combinator_interface, ).
			-define(import_parsec_pos_interface, ).
			-define(import_parsec_error_interface, ).
			-define(import_parsec_char_interface, ).
		-endif.
	-endif.

	-include("parsec_prim.hrl").
	-include("parsec_combinator.hrl").
	-include("parsec_pos.hrl").
	-include("parsec_error.hrl").
	-include("parsec_char.hrl").

Cheers,

David Mercer

> -----Original Message-----
> From:  [mailto:] On
> Behalf Of Joel Reymont
> Sent: Monday, June 08, 2009 11:00 AM
> To: Robert Virding
> Cc: Erlang
> Subject: Re: [erlang-questions] import/1
> 
> 
> On Jun 8, 2009, at 4:57 PM, Robert Virding wrote:
> 
> > Personally I only use it for importing functions from some very basic
> > modules like lists.
> 
> 
> I was trying to import all functions from a module to simplify a DSL.
> 
> Alas, it seems like I either need to use prefixes or import all
> functions individually.
> 
> ---
> Mac hacker with a performance bent
> http://www.linkedin.com/in/joelreymont
> 
> 
> ________________________________________________________________
> erlang-questions mailing list. See http://www.erlang.org/faq.html
> erlang-questions (at) erlang.org



More information about the erlang-questions mailing list