What is the shortest notation to define an operator as a method alias in Scala?

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.


def :=[Prop <: Property[_]](prop: Prop) = register(prop)

应该管用。


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.


You can do this:

def := : Prop => Prop = register

So basically here you define a function of type (Prop => Prop) that just references another function.

链接地址: http://www.djcxy.com/p/56260.html

上一篇: 没有面包屑支持日食

下一篇: 在Scala中将操作符定义为方法别名的最简短符号是什么?