对于无形的新手,我对使用需要一些依赖性的多态函数有疑问.我基本上有这个代码,并希望从run方法中拉出somePoly对象:
import shapeless._ object SomeObject { type SomeType = Int :+: String :+: (String, Int) :+: CNil def run( someList: List[SomeType], someInt:Int, someWord:String ) = { object somePoly extends Poly1 { implicit def doIt = at[Int]( i => i + someInt + someWord.length) implicit def doIt2 = at[String]( i => i.length + someWord.length) implicit def doIt3 = at[(String, Int)]( i => i._1.length + someWord.length) } someList.map( _.map(somePoly) ) } }
我想这样做的一种方式就是这样,但它似乎很乱:
object TypeContainer { type SomeType = Int :+: String :+: (String, Int) :+: CNil } case class SomePolyWrapper( someList: List[TypeContainer.SomeType], someInt:Int, someWord:String ){ object somePoly extends Poly1 { implicit def doIt = at[Int]( i => i + someInt + someWord.length) implicit def doIt2 = at[String]( i => i.length + someWord.length) implicit def doIt3 = at[(String, Int)]( i => i._1.length + someWord.length) } } object SomeObject { def run( someList: List[TypeContainer.SomeType], someInt:Int, someWord:String ) = { val somePolyWrapper = SomePolyWrapper(someList, someInt, someWord) someList.map( _.map(somePolyWrapper.somePoly) ) } }
有人有什么建议吗?