I missed haskell's convenient operator $ so I have decided to introduce one.
class Applayable[-R,T] (val host : Function[R,T]) {
def $: R=>T = host.apply
}
implicit def mkApplayable[R,T] (k : Function[R,T]) : Applayable[R,T] = new Applayable(k)
It has perfrectly worked for
val inc : Int => Int = _ + 1
inc $ 1
but failed for
def inc(x:Int) : Int = x+1
inc $ 1
What type should I specify for implicit cast to convert def definition to an Applayable instance?
You cannot specify a type to do what you want: methods are not functions. You can transform a method into a (possibly partially applied) function by appending the magical underscore after it, like this:
def inc(x:Int) : Int = x+1
(inc _) $ 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With