TomasMikula / pascal

Concise syntax for polymorphic values in Scala 2.x.
MIT License
53 stars 7 forks source link

Override polymorphic methods #3

Open carymrobbins opened 6 years ago

carymrobbins commented 6 years ago

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?

TomasMikula commented 6 years ago

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.