我正在使用两个定义字符串插值器的库(为了清楚起见,简化了代码):
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)
无论如何要从隐式范围中隐藏/删除字符串插值器?