Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

don't understand scalaz endo function

In scalaz, the endo function in Function1Ops is implemented this way:

def endo(implicit ev: R =:= T): Endo[T] =
  Endo.endo(t => ev(self(t)))

I am curious why in the body of Endo.endo function, not just simply taking the self... as Endo.endo(self), which behaves the same as Endo.endo(t=> ev(self(t))).

Here is my mimic implementation and I see no difference between the two. Did I miss something?

def endo[R, T](f: R => T)(implicit ev: T =:= R) = (x: R)=> ev(f(x))
def endo2[R, T](f: R => T)(implicit ev: T =:= R) = f 

Besides, doesn't the first implementation add some overhead at runtime?

like image 269
chenhry Avatar asked Oct 04 '22 23:10

chenhry


1 Answers

The Endo.endo function requires a A => A. The self value is a function T => R which does not comply with the Endo requirement.

You could in theory cast T => R to T => T but the ev parameter is created so you don't need to cast and accidentally make the mistake that T => R is not equal to T => T.

They however could have written it like this:

def endo(implicit ev: R =:= T): Endo[T] =
  Endo.endo(self andThen ev)

Your examples compile because the returntype is not set.

like image 85
EECOLOR Avatar answered Oct 14 '22 07:10

EECOLOR