如何解决阴影/删除字符串插值器
我正在使用两个定义字符串插值器的库(为简化起见,简化了代码):
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)
反正有没有从隐式作用域中隐藏/删除字符串插值器的方法?
解决方法
您的问题显然是ipv4
和ipv6
的插值器冲突,并且编译器不知道要使用哪个插值器。
冲突隐式问题可以通过为隐式之一提供更高的优先级来解决。这可以通过使用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
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。