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?
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.
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