在Elixir中,有什么方法可以让模块列出其功能? [英] In Elixir, is there any way to get a module to list its functions?

查看:62
本文介绍了在Elixir中,有什么方法可以让模块列出其功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与我们可以在Ruby中获取任何对象(或类)以列出其方法的方式相同,Elixir中是否有任何函数可以列出属于模块的所有函数? String.functions (至少可以在远程使用)(其中 String 可以用任何其他模块名称代替)吗?

In the same way that we can get any object (or class) in Ruby to list its methods, is there any function in Elixir to list all functions belonging to a module? Something (at least remotely) like String.functions (where String could be replaced by any other module name)?

推荐答案

Elixir中的每个模块都定义了一个 __ info __ 函数,您可以调用该函数来获取有关该模块的信息。

Each module in Elixir defines an __info__ function you can call to get information about that module.

根据 Elixir Docs,1.6.6例如,您可以将其传递给:functions 以获得模块包含的功能列表。

According the Elixir Docs, 1.6.6 e.g., you can pass it :functions to get a list of functions that module contains.

Map.__info__(:functions)

[delete: 2, drop: 2, equal?: 2, fetch: 2, fetch!: 2, from_struct: 1, get: 2,
 get: 3, has_key?: 2, keys: 1, merge: 2, merge: 3, new: 0, pop: 2, pop: 3,
 put: 3, put_new: 3, size: 1, split: 2, take: 2, to_list: 1, update: 4,
 update!: 3, values: 1]

这篇关于在Elixir中,有什么方法可以让模块列出其功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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