Open jhegedus42 opened 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]))
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 = ???
@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?
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 ...
Full code: https://github.com/jhegedus42/autowire/blob/9662a1bf1c97e65fe16d29c2243dc440b86deb3b/autowire/shared/src/test/scala/autowire/UpickleTests.scala#L24
Gives:
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 forT
.How can I do that ?
This is how the macro generated route looks like: