kind-projector icon indicating copy to clipboard operation
kind-projector copied to clipboard

Alternative syntax for polymorphic lambdas (for Shapeless)

Open yangzai opened this issue 5 years ago • 3 comments

Currently kind-projector allows

val g = λ[Id ~> Option].run(x => Some(x))

to be rewritten as

val g = new (Id ~> Option) {
  def run[A](x: Id[A]): Option[A] = Some(x)
}

I was wondering if there could be an alternative syntax that rewrites to

object g extends (Id ~> Option) {
  def apply[A](x: Id[A]): Option[A] = Some(x)
  
// if possible it should allow rewrite to other method names as well:
//  def run[A](x: Id[A]): Option[A] = Some(x)
}

in situations where the singleton type g.type is required.

i.e. in Shapeless

the[Case1.Aux[g.type, Id[Int], Option[Int]]]

should compile.

yangzai avatar Mar 11 '20 06:03 yangzai

this suggestion doesn't seem to have any attracted much interest. should we leave the ticket open?

SethTisue avatar Nov 30 '20 18:11 SethTisue

In

val g = new (Id ~> Option) {
  def run[A](x: Id[A]): Option[A] = Some(x)
}

The static type is Id ~> Option. If you need a singleton type have you seen if the following works?

val gDefn = new (Id ~> Option) {
  def run[A](x: Id[A]): Option[A] = Some(x)
}
val g: gDefn.type = gDefn
the[Case1.Aux[g.type, Id[Int], Option[Int]]]

dwijnand avatar Mar 23 '21 12:03 dwijnand

@dwijnand nope. I still can't get it to work. I'm testing with:

object Shape extends App {
  import shapeless._
  import shapeless.PolyDefns._

  object G extends (Id ~> Option) {
    def apply[A](x: Id[A]): Option[A] = Some(x)
  }

  val gDefn = new (Id ~> Option) {
//    def run[A](x: Id[A]): Option[A] = Some(x)
    override def apply[T](x: Id[T]): Option[T] = Some(x)
  }
  val g: gDefn.type = gDefn

  the[Case1.Aux[G.type, Id[Int], Option[Int]]] //compiles
  the[Case1.Aux[g.type, Id[Int], Option[Int]]] //does not compile
  the[Case1.Aux[gDefn.type, Id[Int], Option[Int]]] //does not compile
}

Error message:

could not find implicit value for parameter t: shapeless.PolyDefns.Case1.Aux[Shape.g.type,shapeless.Id[Int],Option[Int]]
  the[Case1.Aux[g.type, Id[Int], Option[Int]]] //does not compile

yangzai avatar Mar 23 '21 15:03 yangzai