[erlang-questions] How to call a module's local function from within an external fun
Tue Nov 29 22:34:59 CET 2011
Thanks for your replies :)
I think I might have solved my problem by simply using macros:
-define(Send(What), test_module:send(S, What)).
-define(Expect(What), test_module:expect(S, What)).
test_module:run_test(TestModulePid, ?TestStart -> ?Send(...), ?Expect(...) end)
It might not be the nicest thing but it seems to solve my problem.
On Sat, Nov 26, 2011 at 8:33 PM, Robert Virding
> 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
> -import(lists, [map/2]).
> 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.
> 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 <francis.joanis@REDACTED>
>> 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
>> 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.
>> erlang-questions mailing list
> erlang-questions mailing list
More information about the erlang-questions