pascal icon indicating copy to clipboard operation
pascal copied to clipboard

Override polymorphic methods

Open carymrobbins opened this issue 7 years ago • 1 comments

Ideally, given this Functor definition -

trait Functor[F[_]] {
  def map[A, B](f: A => B)(fa: F[A]): F[B]
}

We could create an instance with pascal via -

  implicit val functorOption: Functor[Option] =
    ν[Functor[Option]].map[A, B](f => fa => fa.map(f))

However, this doesn't work. We have to define Functor like this instead -

trait Functor[F[_]] {
  def map[A, B]: (A => B) => F[A] => F[B]
}

Then the instance compiles. Ideally, we should be able to define it as a polymorphic method and the rewrites should be able to handle it accordingly via an override. Maybe this becomes difficult since I believe this all happens before typer, which means we can't easily access the method signature to override. In that case, maybe there's an alternate way to represent such an override?

carymrobbins avatar Jul 01 '18 17:07 carymrobbins

From

ν[Functor[Option]].map[A, B](f => fa => fa.map(f))

not only can the plugin not tell whether the map signature is

def map[A, B](f: A => B)(fa: F[A]): F[B]

or

def map[A, B](f: A => B): F[A] => F[B]

or

def map[A, B]: (A => B) => F[A] => F[B]

but the plugin is not even able to infer parameter types and return type. For return type it is not a problem: we just omit it in the generated code:

new Functor[Option] {
  def map[A, B] = f => fa => fa.map(f)
}

But parameter types would have to be specified in the generated code.

TomasMikula avatar Jul 01 '18 20:07 TomasMikula