阴影/移除字符串插值器



我使用两个库来定义字符串插值器(为了清晰起见,简化了代码(:

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)

是否存在从隐式作用域阴影/移除字符串插值器的方法?

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

冲突隐含者问题可以通过给予其中一个隐含者更高的优先级来解决。这可以通过将优先级较低的隐词放入特征中,然后扩展声明优先级较高的隐词的对象来实现。

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

相关内容

  • 没有找到相关文章

最新更新