Shadow/Remove 字符串插值器

Shadow/Remove string interpolator

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

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)

是否有 shadow/remove 来自隐式作用域的字符串插值器?

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

冲突的隐含问题可以通过给予其中一个隐含更高的优先级来解决。这可以通过使用 lower-priority implicits into a trait and then extend an object, that declares higher-priority implicits.

来完成

来自 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

}