What type of function does it have?

I skipped haskell the convenient $ operator, so I decided to enter it.

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 worked correctly for

 val inc : Int => Int = _ + 1 inc $ 1 

but failed to execute

 def inc(x:Int) : Int = x+1 inc $ 1 

What type should I specify for implicit conversion in order to convert the definition of def to an instance of Applayable?

+4
source share
2 answers

You cannot specify a type to do what you want: methods are not functions. You can convert the method into a (possibly partially applied) function by adding a magical underline after it, for example:

 def inc(x:Int) : Int = x+1 (inc _) $ 1 
+7
source

you need to treat the inc method as a function by adding "_". It works:

 inc _ $ 1 
+3
source

All Articles