你能用exe(M,F,A)在erlang中为函数参数指定一个类型吗?

时间:2017-02-12 10:23:45

标签: erlang

在下面的例子中,我想在apply(M, F, A)调用中为参数指定一个类型,但我无法弄清楚如何。在此处,dialyzer并未抱怨{event, "something append !"}规范中{anyevent, string()}callback_function类型定义之间的类型不匹配:

-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).

或者您是否有任何其他设计方案可用于对回调函数进行类型检查?

谢谢!

1 个答案:

答案 0 :(得分:1)

原样使用apply/3,我相信你运气不好。 但是,您可以更改该行:

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

为:

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

这将使Dialyzer知道指定的参数类型。

相关问题