![implicit implicit]()
如果您愿意,可以将匿名类替换为命名类 def toCaseClass [A] =新的PartiallyApplied [A]类PartiallyApplied [A] {def适用...} 或者,您可以定义一个类型类(尽管这有点麻烦) trait ToCaseClass [A] {L型def toCaseClass(l:L):A}对象ToCaseClass {类型Aux [A,L0] = ToCaseClass [A] {类型L = L0}def instance [A,L0](f:L0 => A):Aux [A,L0] = new ToCaseClass [A] {类型L = L0覆盖def toCaseClass(l:L0):A = f(l)}隐式def mkToCaseClass [A,R< ;: HList,L< ;: HList](隐式g:Generic.Aux [A,R],r0:Reverse.Aux [R,L],r:Reverse.Aux [L,R]):Aux [A,L] = instance(l => g.from(l.reverse))}def toCaseClass [A](隐式tcc:ToCaseClass [A]):tcc.L =>A = tcc.toCaseClasstoCaseClass [Result] .apply(b :: a :: HNil) 使用类型类隐藏多个隐式:如何在Scala中将具有隐式的方法与另一个方法包装在一起? 您可以在类型宇航员中找到问题的答案: https://books.underscore.io/shapeless-guide/shapeless-guide.html#sec:ops:migration (6.3案例研究:案例类迁移)请注意, IceCreamV1("Sundae",1,true).migrateTo [IceCreamV2a] 使用单个类型参数.您使用 GraphOps 编写的代码由于某些原因而无法正常工作.首先, shapeless.Lazy 不仅仅是包装.这是基于宏的类型类以处理发散的隐式扩展";(在 Scala 2.13 中,有按名称 => 的隐式表示,尽管它们并不等效到 Lazy ).当您了解为什么需要它时,应该使用 Lazy .第二,您似乎定义了一些隐式转换(隐式视图, Mat => A ),但是隐式转换的解析度比其他隐式转换(1 2 3 4 ).第三,您似乎以为在定义时 隐式def foo:Foo = ???def useImplicitFoo(隐式foo1:Foo)= ??? foo1 是 foo .但是通常这是不正确的. foo 在当前范围内定义,并且 foo1 将在 useImplicitFoo 调用站点的范围内解析: 基于类型类设置抽象类型 使用隐式解析时类型参数,为什么val放置很重要?( implicit x:X 和 implicitly [X] 之间的差异)因此,当您调用 toCaseClass 时,隐式 createConverter 不在范围内.代码的固定版本为 trait RunnableGraph [Mat] {def mapMaterializedValue [A](a:Mat => A):RunnableGraph [A]}案例类Wrapper [A,B](值:A => B)隐式类GraphOps [Mat< ;: HList](g:RunnableGraph [Mat]){val ops =这个隐式def createConverter [A,RL< ;: HList](隐式r:Reverse.Aux [Mat,RL],gen:Generic.Aux [A,RL],):包装器[Mat,A] =包装器{l =>val x:RL = l.反向值y:A = gen.from(x)gen.from(l.reverse)}def toCaseClass [A](隐式转换:Wrapper [Mat,A]):RunnableGraph [A] = {g.mapMaterializedValue(convert.value)}}val g:RunnableGraph [B :: A :: HNil] = ???val ops = g.ops导入操作_g.toCaseClass [结果] 尝试 导入akka.stream.scaladsl.RunnableGraph导入shapeless.{::,Generic,HList,HNil}导入shapeless.ops.hlist.Reverse隐式类GraphOps [Mat< ;: HList,R< ;: HList](g:RunnableGraph [Mat]){def toCaseClass [A](隐式r:Reverse.Aux [Mat,R],gen:Generic.Aux [A,R]):RunnableGraph [A] = g.mapMaterializedValue(l => gen.from(l.reverse))}案例类Result(一个:字符串,两个:Int)val g:RunnableGraph [Int :: String :: HNil] = ???g.toCaseClass [结果] I'm using Shapeless to accumulate materialized values in Akka as an HList and convert that to a case class.(You don't have to know Akka much for this question, but the default approach accumulates materialized values as recursively nested 2-tuples, which isn't much fun, so Shapeless HLists seemed a more sensible approach -- and works pretty well. But I don't know how to properly re-use that approach. Here, I'll simplify the kinds of values Akka produces.)For example, let's say we've got two materialized types, "A" and "B":case class Result(b: B, a: A)createA .mapMaterialized((a: A) => a :: HNil) .viaMat(flowCreatingB)((list1, b: B) => b :: list1) .mapMaterialized(list2 => Generic[Result].from(list2)) // list1 = A :: HNil// list2 = B :: A :: HNil... and that produces Result just fine. But it requires that your case class be written backwards -- first value last, etc -- which is kind of dorky and hard to follow.So the sensible thing is to reverse the list before converting to the case class, like this:case class Result(a: A, b: B)// ... .mapMaterialized(list2 => Generic[Result].from(list2.reverse))Now we can think about Result properties in the same order they were built. Yay.But how to simplify and reuse this line of code?The problem is that implicits don't work on multiple type parameters. For example:def toCaseClass[A, R <: HList](implicit g: Generic.Aux[A, R], r: Reverse.Aux[L, R]): R => A = l => g.from(l.reverse) I'd need to specify both A (Result, above) and the HList being built: .mapMaterialized(toCaseClass[Result, B :: A :: HNil])Obviously, that invocation is going to be absurd with long lists (and Akka tends to build up really ugly-looking materialized types, not merely "A" and "B"). It'd be nicer to write something like: .mapMaterialized(toCaseClass[Result])I've tried to solve this using implicits, like this: implicit class GraphOps[Mat <: HList](g: RunnableGraph[Mat]) { implicit def createConverter[A, RL <: HList](implicit r: Reverse.Aux[Mat, RL], gen: Generic.Aux[A, RL]): Lazy[Mat => A] = Lazy { l => val x: RL = l.reverse val y: A = gen.from(x) gen.from(l.reverse) } def toCaseClass[A](implicit convert: Lazy[Mat => A]): RunnableGraph[A] = { g.mapMaterializedValue(convert.value) }But the compiler complains "No implicit view available".The deeper problem is that I don't quite understand how to properly infer...// R = Reversed order (e.g. B :: A :: NHNil)// T = Type to create (e.g. Result(a, b))// H = HList of T (e.g. A :: B :: HNil)gen: Generic.Aux[T, H] // Generic[T] { type Repr = H }rev: Reverse.Aux[R, H] // Reverse[R] { type Out = H }This is sort of backwards from how Shapeless likes to infer things; I can't quite chain the abstract type members properly.Profound thanks if you have insight here.My bad: the example above, of course, requires Akka to compile. A simpler way of putting it is this (with thanks to Dymtro): import shapeless._ import shapeless.ops.hlist.Reverse case class Result(one: String, two: Int) val results = 2 :: "one" :: HNil println(Generic[Result].from(results.reverse)) // this works: prints "Result(one,2)" case class Converter[A, B](value: A => B) implicit class Ops[L <: HList](list: L) { implicit def createConverter[A, RL <: HList](implicit r: Reverse.Aux[L, RL], gen: Generic.Aux[A, RL]): Converter[L, A] = Converter(l => gen.from(l.reverse)) def toClass[A](implicit converter: Converter[L, A]): A = converter.value(list) } println(results.toClass[Result]) // error: could not find implicit value for parameter converter: // Converter[Int :: String :: shapeless.HNil,Result]Dymtro's final example, below...implicit class GraphOps[Mat <: HList, R <: HList](g: RunnableGraph[Mat]) { def toCaseClass[A](implicit r: Reverse.Aux[Mat, R], gen: Generic.Aux[A, R] ): RunnableGraph[A] = g.mapMaterializedValue(l => gen.from(l.reverse)) }... does seem to do what I'd been hoping for. Thank you very much Dmytro!(Note: I had been somewhat misled in analyzing it earlier: it seems IntelliJ's presentation compiler incorrectly insists it won't compile (missing implicits). Moral: Don't trust IJ's presentation compiler.) 解决方案 If I understood correctly you wish that indef toCaseClass[A, R <: HList, L <: HList](implicit g: Generic.Aux[A, R], r: Reverse.Aux[L, R]): L => A = l => g.from(l.reverse)you could specify only A and then R, L be inferred.You can do this with PartiallyApplied patternimport shapeless.ops.hlist.Reverseimport shapeless.{Generic, HList, HNil}def toCaseClass[A] = new { def apply[R <: HList, L <: HList]()(implicit g: Generic.Aux[A, R], r0: Reverse.Aux[R, L], r: Reverse.Aux[L, R] ): L => A = l => g.from(l.reverse)}class Aclass Bval a = new Aval b = new Bcase class Result(a: A, b: B)toCaseClass[Result]().apply(b :: a :: HNil)(without implicit r0 type parameter L can't be inferred upon call of .apply() because L becomes known only upon call .apply().apply(...))or betterdef toCaseClass[A] = new { def apply[R <: HList, L <: HList](l: L)(implicit g: Generic.Aux[A, R], r: Reverse.Aux[L, R] ): A = g.from(l.reverse)}toCaseClass[Result](b :: a :: HNil)(here we don't need r0 because L becomes known already upon call .apply(...)).If you want you can replace anonymous class with named onedef toCaseClass[A] = new PartiallyApplied[A]class PartiallyApplied[A] { def apply...}Alternatively you can define a type class (although this is a little more wordy)trait ToCaseClass[A] { type L def toCaseClass(l: L): A}object ToCaseClass { type Aux[A, L0] = ToCaseClass[A] { type L = L0 } def instance[A, L0](f: L0 => A): Aux[A, L0] = new ToCaseClass[A] { type L = L0 override def toCaseClass(l: L0): A = f(l) } implicit def mkToCaseClass[A, R <: HList, L <: HList](implicit g: Generic.Aux[A, R], r0: Reverse.Aux[R, L], r: Reverse.Aux[L, R] ): Aux[A, L] = instance(l => g.from(l.reverse))}def toCaseClass[A](implicit tcc: ToCaseClass[A]): tcc.L => A = tcc.toCaseClasstoCaseClass[Result].apply(b :: a :: HNil)Hiding several implicits with a type class: How to wrap a method having implicits with another method in Scala?You could find an answer to your question in Type Astronaut:https://books.underscore.io/shapeless-guide/shapeless-guide.html#sec:ops:migration (6.3 Case study: case class migrations)Notice that IceCreamV1("Sundae", 1, true).migrateTo[IceCreamV2a] takes a single type parameter.Your code with GraphOps doesn't work for several reasons.Firstly, shapeless.Lazy is not just a wrapper. It's a macro-based type class to handle "diverging implicit expansion" (in Scala 2.13 there are by-name => implicits for that, although they are not equivalent to Lazy). You should use Lazy when you understand why you need it.Secondly, you seem to define some implicit conversion (implicit view, Mat => A) but resolution of implicit conversions is trickier than resolution of other implicits (1 2 3 4 5).Thirdly, you seem to assume that when you defineimplicit def foo: Foo = ???def useImplicitFoo(implicit foo1: Foo) = ???foo1 is foo. But generally this is not true. foo is defined in current scope and foo1 will be resolved in the scope of useImplicitFoo call site:Setting abstract type based on typeclassWhen doing implicit resolution with type parameters, why does val placement matter? (difference between implicit x: X and implicitly[X])So implicit createConverter is just not in scope when you call toCaseClass.Fixed version of your code istrait RunnableGraph[Mat]{ def mapMaterializedValue[A](a: Mat => A): RunnableGraph[A]}case class Wrapper[A, B](value: A => B)implicit class GraphOps[Mat <: HList](g: RunnableGraph[Mat]) { val ops = this implicit def createConverter[A, RL <: HList](implicit r: Reverse.Aux[Mat, RL], gen: Generic.Aux[A, RL], ): Wrapper[Mat, A] = Wrapper { l => val x: RL = l.reverse val y: A = gen.from(x) gen.from(l.reverse) } def toCaseClass[A](implicit convert: Wrapper[Mat, A]): RunnableGraph[A] = { g.mapMaterializedValue(convert.value) }}val g: RunnableGraph[B :: A :: HNil] = ???val ops = g.opsimport ops._g.toCaseClass[Result]Tryimport akka.stream.scaladsl.RunnableGraphimport shapeless.{::, Generic, HList, HNil}import shapeless.ops.hlist.Reverseimplicit class GraphOps[Mat <: HList, R <: HList](g: RunnableGraph[Mat]) { def toCaseClass[A](implicit r: Reverse.Aux[Mat, R], gen: Generic.Aux[A, R] ): RunnableGraph[A] = g.mapMaterializedValue(l => gen.from(l.reverse)) }case class Result(one: String, two: Int)val g: RunnableGraph[Int :: String :: HNil] = ???g.toCaseClass[Result] 这篇关于反向HList并转换为类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-19 02:33