lihaoyi / autowire

Macros for simple/safe RPCs between Scala applications, including ScalaJS/ScalaJVM
378 stars 48 forks source link

simple example does not compile when using type parameters : could not find implicit value for evidence parameter of type MyReader[T] #61

Open jhegedus42 opened 7 years ago

jhegedus42 commented 7 years ago
      trait MyApi {
        val subApiString: SubApi[String]
      }

      trait SubApi[T] {
        def fooFighter(s: T): String // does not compile
//        def fooFighterString(s:String): String // compiles fine
      }

      // server-side implementation, and router
      object MyApiImpl extends MyApi {

        override val subApiString: SubApi[String] = null
      }

Full code: https://github.com/jhegedus42/autowire/blob/9662a1bf1c97e65fe16d29c2243dc440b86deb3b/autowire/shared/src/test/scala/autowire/UpickleTests.scala#L24

Gives:

Error:(54, 101) could not find implicit value for evidence parameter of type MyReader[T]
        val routes: PartialFunction[Core.Request[String], Future[String]] = MyServerObj.route[MyApi](MyApiImpl)
Error:(54, 101) not enough arguments for method read: (implicit evidence$2: MyReader[T])T.
Unspecified value parameter evidence$2.
        val routes: PartialFunction[Core.Request[String], Future[String]] = MyServerObj.route[MyApi](MyApiImpl)
Error:(54, 101) type mismatch;
 found   : T @unchecked
 required: String
        val routes: PartialFunction[Core.Request[String], Future[String]] = MyServerObj.route[MyApi](MyApiImpl)

Any idea how to solve this issue ?

I guess the macro should understand that T is a String, or put a context bound requiring a type class instance for T .

How can I do that ?

This is how the macro generated route looks like:

Warning:scala: (RES,(<empty> match {
  case autowire.Core.Request(Seq("autowire", "Test", "MyApi", "subApiString", "fooFighter"), (args$macro$1 @ _)) => autowire.Internal.doValidate({
    <synthetic> <artifact> val x$2 = autowire.Internal.read[String, T](args$macro$1, scala.util.Left(autowire.Error.Param.Missing("s")), "s", ((x$1) => MyServerObj.read[T](x$1)));
    Nil.$colon$colon(x$2)
  }) match {
    case scala.$colon$colon((s @ (_: T @unchecked)), Nil) => scala.concurrent.Future(MyApiImpl.subApiString.fooFighter(s)).map(((x$3) => MyServerObj.write(x$3)))
    case _ => $qmark$qmark$qmark
  }
  case autowire.Core.Request(Seq("autowire", "Test", "MyApi", "subApiString", "fooFighter"), (args$macro$2 @ _)) => autowire.Internal.doValidate({
    <synthetic> <artifact> val x$5 = autowire.Internal.read[String, T](args$macro$2, scala.util.Left(autowire.Error.Param.Missing("s")), "s", ((x$4) => MyServerObj.read[T](x$4)));
    Nil.$colon$colon(x$5)
  }) match {
    case scala.$colon$colon((s @ (_: T @unchecked)), Nil) => scala.concurrent.Future(MyApiImpl.subApiString.fooFighter(s)).map(((x$6) => MyServerObj.write(x$6)))
    case _ => $qmark$qmark$qmark
  }
  case autowire.Core.Request(Seq("autowire", "Test", "MyApi", "subApiString", "fooFighter"), (args$macro$3 @ _)) => autowire.Internal.doValidate({
    <synthetic> <artifact> val x$8 = autowire.Internal.read[String, T](args$macro$3, scala.util.Left(autowire.Error.Param.Missing("s")), "s", ((x$7) => MyServerObj.read[T](x$7)));
    Nil.$colon$colon(x$8)
  }) match {
    case scala.$colon$colon((s @ (_: T @unchecked)), Nil) => scala.concurrent.Future(MyApiImpl.subApiString.fooFighter(s)).map(((x$9) => MyServerObj.write(x$9)))
    case _ => $qmark$qmark$qmark
  }
  case autowire.Core.Request(Seq("autowire", "Test", "MyApi", "subApiString", "fooFighter"), (args$macro$4 @ _)) => autowire.Internal.doValidate({
    <synthetic> <artifact> val x$11 = autowire.Internal.read[String, T](args$macro$4, scala.util.Left(autowire.Error.Param.Missing("s")), "s", ((x$10) => MyServerObj.read[T](x$10)));
    Nil.$colon$colon(x$11)
  }) match {
    case scala.$colon$colon((s @ (_: T @unchecked)), Nil) => scala.concurrent.Future(MyApiImpl.subApiString.fooFighter(s)).map(((x$12) => MyServerObj.write(x$12)))
    case _ => $qmark$qmark$qmark
  }
}: autowire.Core.Router[String]))
jhegedus42 commented 7 years ago

this even simpler code has similar issues :


      trait MyApi {
        def foo[T:MyReader](t:T):String
      }

      // server-side implementation, and router
      object MyApiImpl extends MyApi {
        override def foo[T:MyReader](t: T): String = ???
      }

      trait MyReader[T] {}

      implicit val mr: MyReader[String] = ???
      implicit val mwsw: MyWriter[String] = ???

      trait MyWriter[T] {}

      // we serialize into String
      trait MyServer extends autowire.Server[String, MyReader, MyWriter] {
        def write[Result: MyWriter](r: Result): String = ???

        def read[Result: MyReader](p: String): Result = ???

      }

  object MyServerObj extends MyServer{

    val routes: PartialFunction[Core.Request[String], Future[String]] = MyServerObj.route[MyApi](MyApiImpl)
  }

gives :

Error:(43, 97) could not find implicit value for evidence parameter of type autowire.TypeParameterTest.MyReader[autowire.TypeParameterTest.MyReader[T]]
    val routes: PartialFunction[Core.Request[String], Future[String]] = MyServerObj.route[MyApi](MyApiImpl)
Error:(43, 97) not enough arguments for method read: (implicit evidence$4: autowire.TypeParameterTest.MyReader[autowire.TypeParameterTest.MyReader[T]])autowire.TypeParameterTest.MyReader[T].
Unspecified value parameter evidence$4.
    val routes: PartialFunction[Core.Request[String], Future[String]] = MyServerObj.route[MyApi](MyApiImpl)
Error:(43, 97) could not find implicit value for evidence parameter of type autowire.TypeParameterTest.MyReader[T]
    val routes: PartialFunction[Core.Request[String], Future[String]] = MyServerObj.route[MyApi](MyApiImpl)
Error:(43, 97) not enough arguments for method read: (implicit evidence$4: autowire.TypeParameterTest.MyReader[T])T.
Unspecified value parameter evidence$4.
    val routes: PartialFunction[Core.Request[String], Future[String]] = MyServerObj.route[MyApi](MyApiImpl)
Error:(43, 97) could not find implicit value for evidence parameter of type autowire.TypeParameterTest.MyReader[(T, autowire.TypeParameterTest.MyReader[T])]
    val routes: PartialFunction[Core.Request[String], Future[String]] = MyServerObj.route[MyApi](MyApiImpl)
Error:(43, 97) not enough arguments for method foo: (implicit evidence$2: autowire.TypeParameterTest.MyReader[(T, autowire.TypeParameterTest.MyReader[T])])String.
Unspecified value parameter evidence$2.
    val routes: PartialFunction[Core.Request[String], Future[String]] = MyServerObj.route[MyApi](MyApiImpl)

the macro here expands into :

Warning:scala: (RES,(<empty> match {
  case autowire.Core.Request(Seq("autowire", "TypeParameterTest", "MyApi", "foo"), (args$macro$1 @ _)) => autowire.Internal.doValidate({
    <synthetic> <artifact> val x$4 = autowire.Internal.read[String, autowire.TypeParameterTest.MyReader[T]](args$macro$1, scala.util.Left(autowire.Error.Param.Missing("evidence$1")), "evidence$1", ((x$2) => TypeParameterTest.this.MyServerObj.read[autowire.TypeParameterTest.MyReader[T]](x$2)));
    {
  <synthetic> <artifact> val x$3 = autowire.Internal.read[String, T](args$macro$1, scala.util.Left(autowire.Error.Param.Missing("t")), "t", ((x$1) => TypeParameterTest.this.MyServerObj.read[T](x$1)));
  Nil.$colon$colon(x$3)
}.$colon$colon(x$4)
  }) match {
    case scala.$colon$colon((evidence$1 @ (_: autowire.TypeParameterTest.MyReader[T] @unchecked)), scala.$colon$colon((t @ (_: T @unchecked)), Nil)) => scala.concurrent.Future(TypeParameterTest.this.MyApiImpl.foo(t, evidence$1)).map(((x$5) => TypeParameterTest.this.MyServerObj.write(x$5)))
    case _ => $qmark$qmark$qmark
  }
  case autowire.Core.Request(Seq("autowire", "TypeParameterTest", "MyApi", "foo"), (args$macro$2 @ _)) => autowire.Internal.doValidate({
    <synthetic> <artifact> val x$9 = autowire.Internal.read[String, autowire.TypeParameterTest.MyReader[T]](args$macro$2, scala.util.Left(autowire.Error.Param.Missing("evidence$1")), "evidence$1", ((x$7) => TypeParameterTest.this.MyServerObj.read[autowire.TypeParameterTest.MyReader[T]](x$7)));
    {
  <synthetic> <artifact> val x$8 = autowire.Internal.read[String, T](args$macro$2, scala.util.Left(autowire.Error.Param.Missing("t")), "t", ((x$6) => TypeParameterTest.this.MyServerObj.read[T](x$6)));
  Nil.$colon$colon(x$8)
}.$colon$colon(x$9)
  }) match {
    case scala.$colon$colon((evidence$1 @ (_: autowire.TypeParameterTest.MyReader[T] @unchecked)), scala.$colon$colon((t @ (_: T @unchecked)), Nil)) => scala.concurrent.Future(TypeParameterTest.this.MyApiImpl.foo(t, evidence$1)).map(((x$10) => TypeParameterTest.this.MyServerObj.write(x$10)))
    case _ => $qmark$qmark$qmark
  }
}: autowire.Core.Router[String]))
jhegedus42 commented 7 years ago

think the problem might be that implicit parameters are not supported by autowire at all : https://gitter.im/scala-js/scala-js?at=58ff1dbc08c00c092ab30da7

and this type class constraint here is implemented by implicit arguments : override def foo[T:MyReader](t: T): String = ???

bbarker commented 6 years ago

@jhegedus42 I think I may have hit this problem as well; hope to look into it more later today. Did you ever find a workaround?

jhegedus42 commented 6 years ago

Sorry for late answer :)

this is an inherent limitation in Autowire ... if you think about it a bit deeper then it is kinda understandable ... it would be pretty difficult to do this ... with macros, or otherwise... sure it is doable ... in principle ... but then the macros would need to look explicitly into the type parameters at compile time ... which they are not doing right now... and probably never will... kinda deal breaker... IMHO ...

so i have a workaround, but it wont help you much, yet : i wrote my own autowire :) sort of... no macros ... no nothing... just type classes and lot of type hackery ... and it is simple REST ... no magic ... i am not sure it was worth it ... but it seems to be working so far... i plan to put it to github once it is in some publishable form... the way i see it that will be 1 year from now :( ... got a bit busy with work ...