[erlang-questions] How to call a module's local function from within an external fun
Sun Nov 27 02:33:37 CET 2011
Sorry, yes but no. There is an -import declaration but it is purely a syntactic feature, it has no semantic meaning at all and provides no extra features. If I do a
then I can call map(Fun,List) directly in my file, but all that happens is that this call is transformed internally into lists:map(Fun,List). And that is all that it does, it is a pure syntactic transformation and adds no extra meaning. Unfortunately.
That is why many recommend you never use it. I personally only use it for importing "standard" functions from commonly used modules, like from lists as in my example.
----- Original Message -----
> There is a built in module attribute, -import, that can be used to
> import functions exported from one module into the local namespace
> of another module. If you really want to write a parse transform I
> would recommend writing one that adds this module attribute to your
> test modules. An alternative solution is to create an include a file
> containing only an -import attribute into the test modules.
> echo '-import(test_module, [send/2, expect/2]). %% fix arities' >
> After this you will able to use test_module:send/2 and
> test_module:expect/2 as send/2 and expect/2 in the modules you add
> the following include attribute to:
> / Magnus
> On Fri, Nov 25, 2011 at 7:29 PM, Francis Joanis <
> > wrote:
> > Hi,
> > I'm trying to write a test tool that can be used to send and expect
> > messages over a custom protocol. I would like the tests themselves
> > to
> > be supplied as funs from outside the module so that:
> > test_module:run_test(TestModulePid, fun() -> send(...), expect(...)
> > end).
> > would end up calling send() and expect() from test_module, as if
> > the
> > fun was executed in the "context" of the module (i.e. as if it were
> > defined directly within run_test() and not supplied to it). The
> > resulting "API" would look like some kind of DSL specialized to
> > define
> > my tests.
> > In my case I need multiple test_modules to run in parallel, so I
> > can't
> > use a single "test_module" registered name. It would be simple to
> > have
> > test_module export both send() and expect() so that the following
> > works:
> > test_module:run_test(TestModulePid, fun(TestModulePid) ->
> > test_module:send(TestModulePid, ...),
> > test_module:expect(TestModulePid, ...) end).
> > but I don't like the extra verboseness of having to write
> > test_module:... all the time.
> > I _think_ that I might be able to use parse transforms to achieve
> > what
> > I want but I wanted to know if there would be an easier way.
> > Cheers,
> > Francis
> > _______________________________________________
> > erlang-questions mailing list
> > http://erlang.org/mailman/listinfo/erlang-questions
> erlang-questions mailing list
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the erlang-questions