scala:具有可变长度参数的Function对象的特征? [英] scala: trait for a Function object with variable length arguments?

查看:62
本文介绍了scala:具有可变长度参数的Function对象的特征?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些Scala代码来模拟python装饰器。我正在考虑通过让装饰器扩展功能特性来实现这一目标。问题是我希望这个装饰器扩展一个可以接受任意数量参数的Function,并且我只能找到的Function trait只允许特定数量的参数,例如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?

编辑:我在 scala:记住一个函数,不管该函数接受多少参数?

推荐答案

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:具有可变长度参数的Function对象的特征?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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