【问题标题】:Get function arity in scala在scala中获取函数arity
【发布时间】:2016-01-31 08:10:21
【问题描述】:

有没有办法检索函数的元数?例如,给定以下函数:

def moop[I,O](func: I => O) = {
  val arity = I.???
  ???
}

我如何获得func 的数量?

【问题讨论】:

  • 怎么可能不是1?
  • 例如I 可以是元组类型
  • AFAIK,元组仍然是一个操作数。
  • 基于arity你想做什么?
  • 由于在原生库中使用,我需要生成一个字面量等于arity的字符串。

标签: scala function arity


【解决方案1】:

如果I是一个元组,你可以在它上面调用继承自ProductproductArity方法。

如果你有一个你只知道它是某种函数的对象,你将不得不检查FunctionN 类的(有限)集合,因为没有像元组那样的通用超类型。看起来我们目前有 Function0Function22

【讨论】:

  • 我想我会求助于 sbt-boilerplate 为所有可能的 arities 生成特定的创建函数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-09
  • 2015-10-07
  • 1970-01-01
  • 1970-01-01
  • 2020-09-19
相关资源
最近更新 更多