知道传递函数的参数数(erlang) [英] Knowing the number of parameters of a passed function (erlang)

查看:166
本文介绍了知道传递函数的参数数(erlang)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ERLANG中:假设我们有一个函数f(),它将F1作为输入,而F1是一个函数。有没有办法知道F1输入参数的数量。

In ERLANG: Assume we have a function f() that takes F1 as inputs where F1 is a function. Is there a way to know the number of input parameters of F1.

我觉得有某种解决方案,但是我不确定。例如:

I feel somehow there IS a solution, but I am not sure. for instance:

 -module(high).
 -compile(export_all).

 f1() -> 1.
 f2(X) -> X. 
 f3(X, Y) -> {X,Y}. 

 run(F) -> io:format("F ~p ~n", [F]).

因此,函数run / 1是否有任何方法可以了解有关传递函数的信息[主要是

So, is there any way for function run/1 to know information about the passed function [mainly the number of input parameters of the passed function].

注意:请注意,这是一个理论问题。
注意:这是在开源中可用的apply(fun,[arguments])的代码。

Note: Please be informed that this is a theoretical question. Note: is the code of apply(fun,[arguments]) available in open-source .. this may hep me I guess.

推荐答案

erlang:fun_info(Fun,arity)。

erlang:fun_info(Fun,arity).

例如

F = fun(A,B) -> A+B end.
#Fun<erl_eval.12.111823515>
3> erlang:fun_info(F,arity).
{arity,2}

这篇关于知道传递函数的参数数(erlang)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆