根据
this question Spray使用sbt-boilerplate生成ApplyConverter实例,隐式转换A :: B :: … :: HNil => R更传统(A,B,…)=> R易于使用.这个问题是在一年前提出来的;现在可以使用宏或Shapeless的新功能来完成这个,这样就不需要外部代码生成步骤吗?
解决方法
这已被包括在无形状中很长一段时间.在无形2.0.0中,您可以执行以下操作,
scala> import shapeless._,syntax.std.function._ import shapeless._ import syntax.std.function._ scala> val f1: (Int,String,Boolean) => Int = (i,s,b) => i+s.length+(if(b) 1 else 0) f1: (Int,Boolean) => Int = <function3> scala> val pf1 = f1.toProduct pf1: Int :: String :: Boolean :: HNil => Int = <function1> scala> pf1(23 :: "foo" :: true :: HNil) res0: Int = 27 scala> val pf2: (Int :: String :: HNil) => Int = l => l.head+l.tail.head.length pf2: Int :: String :: HNil => Int = <function1> scala> val f2 = pf2.fromProduct f2: (Int,String) => Int = <function2> scala> f2(23,"foo") res1: Int = 26
(REPL结果类型呈现为了可读性而整理).