Method parameterized by the type of the return method of the class

Is it possible to capture the return type of a method of an uninitialized class of a class? I want to create a shell that will work as follows:

abstract class Node[L, R]{
    def call(args: L): R
}
case class Kls(arg1: Int, arg2: Int) {
    def apply() = arg1 + arg2
}

object Node {
def apply[L <: { def apply(): R }, R](implicit lgen: LabelledGeneric[L]): Node[lgen.Repr, R] = {
  new Node[lgen.Repr, R] {
    def call(args: lgen.Repr): R = {
      lgen.from(args).apply()
    }
  }
}
}

val n = Node[Kls] // does not compile - missing type param R
n.call(arg1 :: arg2 :: HNil) //should have the right return type

Alternatively, is there an FnTo Labeled product ? Which macro fu do I need to create?

+4
source share
2 answers

I solved this with a simple macro:

trait CallApply[C] {
  type Ret
  def apply(c: C): Ret
}

object CallApply {
  type Aux[C, R] = CallApply[C] { type Ret = R }

  implicit def materialize[C, R]: Aux[C, R] = macro CallApplyImpl.materialize[C]
}

object CallApplyImpl {
  import scala.reflect.macros.whitebox

  def materialize[C: c.WeakTypeTag](c: whitebox.Context): c.Tree = {
    import c.universe._

    val C = weakTypeOf[C]
    val assignM = C.decls.collect {
      case sym: MethodSymbol if sym.name == TermName("apply") => sym
    }
    if (assignM.headOption.isEmpty) c.abort(c.enclosingPosition, "case class must define an apply() method")

    val R = assignM.head.returnType
    q"""new _root_.fwb.api.CallApply[$C] { type Ret = $R; def apply(c: $C) : $R = c.apply() }"""
  }
}

Using:

  object Node {
    def call[L](implicit lgen: LabelledGeneric[L], ev: CallApply[L]): Node[lgen.Repr, ev.Ret] = {
      new Node[lgen.Repr, ev.Ret] {
        def call(args: lgen.Repr): ev.Ret = {
          ev(lgen.from(args))
        }
      }
    }
  }

And it val n = Node[Kls]works as expected. However, it would be nice to see a solution without metaprogramming (if possible).

0
source

Perhaps I misunderstand, but I do not think you need Lat all.

case class Kls(arg1: Int, arg2: Int) {
  def apply() = arg1 + arg2
}

abstract class Node[L, R]{
  def call(args: L): R
}

import shapeless._

object Node {
  def apply[R](implicit gen: LabelledGeneric[R]): Node[gen.Repr, R] =
    new Node[gen.Repr, R] {
      def call(args: gen.Repr): R = gen.from(args)
    }
}

And then:

import shapeless.syntax.singleton._

val n = Node[Kls]

val result = n.call('arg1 ->> 1 :: 'arg2 ->> 2 :: HNil)

result Kls. , ?

0

All Articles