繁体   English   中英

Scala中所有函数的超类型是什么?

[英]What is the supertype of all functions in Scala?

我知道我可以对Function1Function2等进行instanceOf检查,但是有一种通用的方法来查看某些东西是否有效(它可以具有仲裁数量的args)。 我试着定义这样的东西:

type FuncType = (Any*) -> Any

但这也不起作用。 基本上我有一些看起来像这样的代码:

call = (name: Any, args: Any*) -> if name.isFunction then name.castAs[Function].apply(args) else name
aFunction = (name: String) => "Hello " + name
notAFunction = "Hello rick"
call(aFunction, "rick")
call(notAFunction)

所有函数类型都没有通用的超类型。

Scala无法对函数的arity进行抽象。 但是,您可以查看Shapeless库,它引入了一个名为HList东西,您可以使用它来抽象函数的arity。

但是,我认为这不是你真正需要的。 听起来你只是想做一个像“这是一个功能吗?”的检查。 你可能会觉得奇怪的是,没有与arity无关的Function超类型,但如果你想对它做一些有用的事情,你几乎总是需要知道一个函数的arity。

或者,您可以使用函数的curried方法执行某些操作,该函数将返回Function1

不,没有办法做到这一点,除了通过并检查每个Function1Function2等。每个特征的AnyRefAnyRef ,它不会帮助你区别于其他任何东西。 每个traits的apply方法都使用不同数量的参数,因此无法为它们提供具有apply方法的父级。 你可能接近你想要做的最接近的是:

def arbitraryFunction(function: AnyRef, args: Seq[Any]): Any = {
  function match {
    case f: Function1[Any, Any] => f(args(0))
    case f: Function2[Any, Any, Any] => f(args(0), args(1))
    // and so on
  }
}

但这是疯狂和危险的,如果类型错误,将在运行时抛出异常,例如

arbitraryFunction((x: Int) => x * 2, List("I'm a String!"))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM