在 Scala 中将运算符定义为方法别名的最短表示法是什么?
鉴于下面的通用 register
方法,我想将 :=
运算符定义为符号别名。
def register[Prop <: Property[_]](prop: Prop): Prop
@inline
final def :=[Prop <: Property[_]] = register[Prop] _
最初我想写这样的东西:
val := = register _
但这给了我函数签名 Nothing =>;什么都没有。我的下一次尝试是使用
Prop
类型对其进行参数化,但这显然只有在我将其设为 def
时才有效,它可以获取类型参数并向前传递它们。
理想情况下,我想省略 @inline
注释,但我不确定 Scala 编译器会从中生成什么目标代码。
最重要我的目标是不让 :=
方法复制 register
方法签名的所有部分(名称除外),然后简单地让前者委托后者。
Given the generic register
method below I would like to define the :=
operator as a symbolic alias.
def register[Prop <: Property[_]](prop: Prop): Prop
@inline
final def :=[Prop <: Property[_]] = register[Prop] _
Originally I wanted to write something like this:
val := = register _
But that gives me the function signature Nothing => Nothing
. My next attempt was to parameterize it with the type Prop
but that apparently works only if I make it a def
, which can take type parameters and pass them onwards.
Ideally I would like to omit the @inline
annotation but I am not sure what object code the Scala compiler makes out of it.
Most important my goal is it not to have the :=
method duplicate all parts of the register
method's signature except for the name and then simply let the former delegate to the latter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
应该有效。
should work.
我认为目前的 Scala 中没有任何方法可以实现您所追求的目标(基本上是 Ruby 中的
alias
为您提供的功能)。 autoproxy 插件 是解决此类问题的尝试,但它是由于在编译器插件中生成代码存在各种问题,尚未真正准备好用于生产使用。I don't believe that there's any way to achieve what you're after (basically what
alias
gives you in Ruby) in Scala as it currently stands. The autoproxy plugin is an attempt to address this kind of problem, but it's not really ready for production use yet due to various issues with generating code in compiler plugins.你可以这样做:
所以基本上在这里你定义了一个类型为 (Prop => Prop) 的函数,它只引用另一个函数。
You can do this:
So basically here you define a function of type (Prop => Prop) that just references another function.