JSM
JSM

Reputation: 47

Can you specify a type for function argument in erlang using apply(M, F, A)?

In the example below, I would like to specify a type for argument in the apply(M, F, A) call, but I can't figure how. Here, dialyzer doesn't complain for type mismatch between {event, "something append !"} and {anyevent, string()} type definition in callback_function spec :

-module(erl_test).

-export([
         callback_function/1,
         test_it/0
        ]).

-spec callback_function(
  Event::{anyevent, string()}) -> ok.
callback_function(Event) ->
  io:format("event received: ~p~n", [Event]),
  ok.

-spec notify_something(CbMod::module(), CbFun::atom()) -> ok.
notify_something(CbMod, CbFun) ->
  apply(CbMod, CbFun, [{event, "something append !"}]),
  ok.

test_it() ->
  notify_something(?MODULE, callback_function).

Or do you have any other design proposition that I could use to do type checking on a callback function ?

Thank you !

Upvotes: 1

Views: 163

Answers (1)

Asier Azkuenaga
Asier Azkuenaga

Reputation: 1189

Using apply/3 as it is, I believe you are out of luck. However, you could change the line:

apply(CbMod, CbFun, [{event, "something append !"}]),

to:

CbMod:CbFun([{event, "something append !"}]),

This would make Dialyzer aware of the specified argument type.

Upvotes: 1

Related Questions