Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 481
Tim Friske Avatar asked Nov 20 '11 23:11

Tim Friske


2 Answers

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

should work.

like image 198
Luigi Plinge Avatar answered Nov 08 '22 09:11

Luigi Plinge


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.

like image 36
Paul Butcher Avatar answered Nov 08 '22 09:11

Paul Butcher