我正在努力学习无形。我有一个玩具项目,但我遇到了一个问题。
sealed trait RunnerType
object RunnerType {
case object BasicRunner extends RunnerType
case object MidComplex extends RunnerType
case object ComplexRunner extends RunnerType
}
trait Runner[Input, Output] {
type T
def inner: Input => Output
def apply(in: Input): Output = inner(in)
}
object Runner {
case class A(inner: Int => String) extends Runner[Int, String] {
type T = RunnerType.BasicRunner.type
}
case class B(inner: Any => Any) extends Runner[Any, Any] {
type T = RunnerType.MidComplex.type
}
case class C(inner: String => (Int => String)) extends Runner[String, (Int => String)] {
type T = RunnerType.BasicRunner.type
}
//DSL
def a(f: Int => String) = A(f)
def b(f: Any => Any) = B(f)
def c(f: String => (Int => String)) = C(f)
// ops...
val list = List(
a(i => i.toString),
c(i => a => i.toString + (a + 10).toString)
, b(a => a)
)
}
正如您所看到的,我的dsl不适合使用,因为我丢失了完全类型,我想通过RunnerType搜索。如果我使用HList,可能我可以保留类型信息。如“
val hlist =
a(i => i.toString) ::: c(i => a => i.toString + (a + 10).toString) ::: b(a => a) ::: HNil
现在我想要一个方法,它将RunnerType作为参数,并返回正确的对象,我可以使用apply方法并知道输入和输出类型。例如:
def lookup[L <: HList](t:RunnerType, hlist: L): Runner[correct_input, correct_output] = // Lookup in hlist for t(runner type)
是否正确使用Hlist?如果是的话,我怎么能用shapeess达到这个目标?