2

我正在使用两个定义字符串插值器的库(为了清楚起见,简化了代码):

Http4s

implicit class LiteralsOps(val sc: StringContext) extends AnyVal {
  def uri(args: Any*): Uri = macro LiteralSyntaxMacros.uriInterpolator
  (...)
  def ipv4(args: Any*): Uri.Ipv4Address = macro LiteralSyntaxMacros.ipv4AddressInterpolator
  def ipv6(args: Any*): Uri.Ipv6Address = macro LiteralSyntaxMacros.ipv6AddressInterpolator
}

ip4s

implicit class IpLiteralSyntax(val sc: StringContext) extends AnyVal {
  def ip(args: Any*): IpAddress = macro LiteralSyntaxMacros.ipInterpolator
  def ipv4(args: Any*): Ipv4Address = macro LiteralSyntaxMacros.ipv4Interpolator
  def ipv6(args: Any*): Ipv6Address = macro LiteralSyntaxMacros.ipv6Interpolator
  (...)
  def host(args: Any*): Hostname = macro LiteralSyntaxMacros.hostnameInterpolator
}

我想同时使用它们,即来自 Http4s 的uri插值器和来自 ip4s 的各种插值器。问题在于以下代码:

import com.comcast.ip4s._
import org.http4s.syntax.literals._

class Foo(cidr: Cidr[Ipv4Address] = ipv4"192.168.1.1" / 24)

编译失败:

[error] Note that implicit conversions are not applicable because they are ambiguous:
[error]  both method IpLiteralSyntax in package ip4s of type (sc: StringContext): com.comcast.ip4s.package.IpLiteralSyntax
[error]  and method http4sLiteralsSyntax in trait LiteralsSyntax of type (sc: StringContext): org.http4s.syntax.LiteralsOps
[error]  are possible conversion functions from StringContext to ?{def ipv4: ?}
[error]   class Foo(cidr: Cidr[Ipv4Address] = ipv4"192.168.1.1" / 24)

无论如何要从隐式范围中隐藏/删除字符串插值器?

4

1 回答 1

1

您的问题显然是ipv4and的插值器存在冲突ipv6,编译器不知道要使用哪一个。

冲突的隐含问题可以通过给隐含更高的优先级之一来解决。这可以通过使用将较低优先级的隐式放入特征然后扩展一个对象来完成,该对象声明了较高优先级的隐式

可以使用 trait 将隐含的内容http4s带入范围AllSyntax

import com.comcast.ip4s._
import org.http4s.Uri
import org.http4s.syntax.AllSyntax

case class Foo(cidr: Cidr[Ipv4Address] = ipv4"192.168.1.1" / 24)

class MyApp extends AllSyntax {

  val testUri: Uri = uri"http://test.pl" //we can still use uri interpolator from http4s

}

但这仍然无法编译:

import org.http4s.Uri
import org.http4s.syntax.AllSyntax
import com.comcast.ip4s._

class MyApp extends AllSyntax {

  val testUri: Uri = uri"http://test.pl"
  val ip = ipv4"192.168.1.1" / 24 //compile error

}

不幸的是,ip4s没有提供任何将隐式带入范围的特征,因此我们可以优先考虑它们。

您可以做的是创建另一个对象,在其中复制包对象的内部结构com.comcast.ip4s,然后扩展 AllSyntax:

import org.http4s.Uri
import org.http4s.syntax.AllSyntax
import com.comcast.ip4s._
import scala.language.experimental.macros

object MySyntax extends AllSyntax {

  //copied from com.comcast.ip4s
  final implicit class IpLiteralSyntax(val sc: StringContext) extends AnyVal {
    def ip(args: Any*): IpAddress = macro LiteralSyntaxMacros.ipInterpolator
    def ipv4(args: Any*): Ipv4Address =
    macro LiteralSyntaxMacros.ipv4Interpolator
    def ipv6(args: Any*): Ipv6Address =
    macro LiteralSyntaxMacros.ipv6Interpolator
    (...)
  }

}

然后你可以像这样使用它:

class MyApp extends App {

  import MySyntax._

  val testUri: Uri = uri"http://test.pl"
  val ip = ipv4"192.168.1.1" / 24 //interpolator from com.comcast.ip4s has higher priority

}
于 2020-09-08T00:15:54.543 回答