小猫 - 不明确的进口 [英] Kittens - ambiguous imports

查看:63
本文介绍了小猫 - 不明确的进口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎完全从此处找到的派生展示"示例中获取了以下代码:https://github.com/milessabin/kittens

I have taken the following code almost entirely from the 'derive show' example found here: https://github.com/milessabin/kittens

import cats._, cats.derived._, cats.implicits._

object Test extends App {

  case class Address(street: String, city: String, state: String)

  case class ContactInfo(phoneNumber: String, address: Address)

  case class People(name: String, age: Double,  contactInfo: ContactInfo)

  val mike = People("Mike", 1.23,  ContactInfo("202-295-3928", Address("1 Main ST", "Chicago", "IL")))

  implicit val addressShow: Show[Address] = new Show[Address] {
    def show(a: Address) = s"${a.street}, ${a.city}, ${a.state}"
  }

//  I would like to use a custom version of Show for Doubles...
//  implicit val doubleShow: Show[Double] = new Show[Double] {
//    def show(d: Double) = s"Custom Double Show: $d"
//  }

  implicit val peopleShow: Show[People] = {
    import auto.show._
    semi.show
  }

  println(mike.show)

}

我希望能够使用 Show[Double] 的自定义版本(我不是真的,但这是一个很好的最小示例,它说明了我实际遇到的问题)

I would like to be able to use a custom version of Show[Double] (I don't really, but this is a nice minimal example which illustrate the problem I'm actually having)

如果我取消注释我的 doubleShow,我会收到以下错误:

If I un-comment my doubleShow I get the following error:

Error:(25, 10) ambiguous implicit values:
 both value emptyProductDerivedShow in trait MkShowDerivation of type => cats.derived.MkShow[shapeless.HNil]
 and method emptyCoproductDerivedShow in trait MkShowDerivation of type => cats.derived.MkShow[shapeless.CNil]
 match expected type cats.derived.MkShow[A]
    semi.show

我怎样才能让它工作?我本来希望我的本地定义的隐式胜过任何从猫进口的......

How can I get this working? I would have expected my locally defined implicits to win out over any imported from cats...

推荐答案

尝试使用隐式对象代替隐式 val

Try implicit object instead of implicit val

implicit object doubleShow extends Show[Double] {
  def show(d: Double) = s"Custom Double Show: $d"
}

或使用隐式 val 并更改导入(不要 importcats.instances.double._)

or use implicit val and change imports (don't import cats.instances.double._)

import cats._, cats.derived._
import cats.syntax.show._
import cats.instances.string._ // People uses String and Double

implicit val doubleShow: Show[Double] = new Show[Double] {
  def show(d: Double) = s"Custom Double Show: $d"
}

这篇关于小猫 - 不明确的进口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆