Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What type does function has?

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?

like image 270
ayvango Avatar asked Dec 28 '22 15:12

ayvango


1 Answers

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
like image 74
Jean-Philippe Pellet Avatar answered Jan 12 '23 21:01

Jean-Philippe Pellet