scala:具有可变长度参数的 Function 对象的特征?

发布于 2024-11-03 06:13:18 字数 200 浏览 1 评论 0原文

我正在编写一些 scala 代码来模拟 python 装饰器。我正在考虑通过让装饰器扩展 Function 特征来实现这一点。问题是我希望这个装饰器扩展一个接受任意数量参数的函数,而我能找到的唯一函数特征只允许特定数量的参数,例如 Function1、Function2 等。

这样的特征是否存在?或者,是否有更好的方法来实现这样的装饰器?

编辑:我在

i'm writing some scala code to emulate a python decorator. i'm considering implementing this by having the decorator extend a Function trait. the issue is that i want this decorator to extend a Function that accepts any number of arguments, and the only Function traits i can find only allow a specific number of arguments, e.g. Function1, Function2, etc.

does such a trait exist? alternatively, is there a better way to implement such a decorator?

Edit: I recast this question to be more clear at scala: memoize a function no matter how many arguments the function takes?.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七色彩虹 2024-11-10 06:13:18
scala> val bar: (Int*) => Int = {args => args.sum}
bar: (Int*) => Int = <function1>

scala> bar(1,2,3)
res4: Int = 6

不幸的是,您不能在这里使用类型推断:

scala> val bar = {args: Int* => args.sum}
<console>:1: error: ';' expected but identifier found.
       val bar = {args: Int* => args.sum}
scala> val bar: (Int*) => Int = {args => args.sum}
bar: (Int*) => Int = <function1>

scala> bar(1,2,3)
res4: Int = 6

Unfortunatelly, you can't use type inference here:

scala> val bar = {args: Int* => args.sum}
<console>:1: error: ';' expected but identifier found.
       val bar = {args: Int* => args.sum}
十六岁半 2024-11-10 06:13:18

我不确定你想要完成什么,但如果你对函数的数量有问题,你可以使用使用 Tuple 作为输入参数的 Function1 。由于任何函数的元组和非元组版本之间的转换都很容易,因此这应该不会太不方便。如果您能更详细地描述您需要的内容,我可以给您一个代码示例。

I'm not sure what you try to accomplish, but if you have problems with the arity of functions, you can work on a Function1 which uses a Tuple as input parameter. As it is easy to convert between tupled and untuples versions for any function, this should be not too inconvenient. I could give you a code example if you would describe more detailed what you need.

半城柳色半声笛 2024-11-10 06:13:18

不存在功能超特征。
您必须使用隐式方法将要装饰的方法包装到自己的类中。
该类必须处理不同的方法类型。
然后你必须创建一些类来生成你的装饰方法。

或者,如果可以的话,您只需创建类似的新方法:

def myF(x:Int,y:Int)={x*y}

def myF2(x:Int,y:Int)={myDecorator(myF(x,y))}
def myDecorator[X](f:=>X)={println("entering");val ret=f();println("exiting");ret}

或尝试:

def myF_old(x:Int,y:Int)=x*y 
def paramDecorator(x:Any*):List[Any]={/*do some match operations or something*/} 
def outputDecorator(x:Any*):Any={/*do some match operations or so*/} 
def myF(x:Int,y:Int):Int={
params=paramDecorator(x,y);
val res=myF_old(params(1),params(2));
outputDecorator(res);
}

There is not a function supertrait.
You will have to wrap the method you want to decorate into an own class with an implicit.
That class gonna have to handle the different method types.
Then you have to create some class what generates your decorated method.

Or if you can you just create new methods like that:

def myF(x:Int,y:Int)={x*y}

def myF2(x:Int,y:Int)={myDecorator(myF(x,y))}
def myDecorator[X](f:=>X)={println("entering");val ret=f();println("exiting");ret}

or try:

def myF_old(x:Int,y:Int)=x*y 
def paramDecorator(x:Any*):List[Any]={/*do some match operations or something*/} 
def outputDecorator(x:Any*):Any={/*do some match operations or so*/} 
def myF(x:Int,y:Int):Int={
params=paramDecorator(x,y);
val res=myF_old(params(1),params(2));
outputDecorator(res);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文