简体   繁体   English

Shapeless zipWithKeys:找不到隐式参数

[英]Shapeless zipWithKeys: Could not find implicit parameter

I've been playing around with shapeless recently. 我最近一直在玩无形状的游戏。 There is this very basic thing I can't wrap my head around: 有一个非常基本的东西我无法解决:

import shapeless._
import syntax.singleton._
import record._

object NotWorking {
  val result = ("value" :: HNil).zipWithKeys("key" :: HNil)
} 

I expect this snippet to output an extensible Record. 我希望该代码片段输出可扩展的Record。 But the compiler is not able to find an implicit for withKeys : 但是编译器无法找到withKeys的隐式:

could not find implicit value for parameter withKeys: shapeless.ops.hlist.ZipWithKeys[shapeless.::[String,shapeless.HNil],shapeless.::[String,shapeless.HNil]]
[error]     ("value" :: HNil).zipWithKeys("key" :: HNil)

It's even more confusing, as the example I took form Shapeless' test cases works perfectly: 更加令人困惑,因为我从Shapeless的测试用例中获得的示例可以完美地工作:

import shapeless._
import syntax.singleton._
import record._

object ShamelesslyStolenFromTests {
  val orig =
    ("intField" ->> 1) ::
    ("boolField" ->> true) ::
    HNil

  val result = orig.values.zipWithKeys(orig.keys)
}

What am I missing? 我想念什么?

The following code compiles: 以下代码编译:

  import shapeless._
  import syntax.singleton._
  import record._

  object FinallyWorking {
    val result = ("value" :: HNil).zipWithKeys[Witness.`"key"`.T :: HNil]("key".narrow :: HNil)
  }

So it seems this was an issue with type inference for type parameter of the method. 因此,似乎这是方法的类型参数的类型推断问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM