Scala 阴影/删除字符串插值器
我使用两个库来定义字符串插值器(为了清晰起见,简化了代码): : : 我想同时使用它们,即来自Http4s的Scala 阴影/删除字符串插值器,scala,implicit,http4s,Scala,Implicit,Http4s,我使用两个库来定义字符串插值器(为了清晰起见,简化了代码): : : 我想同时使用它们,即来自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 im
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
的插值器存在冲突,编译器不知道使用哪一个
冲突隐式ITS问题可以通过赋予其中一个隐式ITS更高的优先级来解决。这可以通过使用推杆来完成
来自http4s
的隐式可以通过traitAllSyntax
纳入范围:
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
的内部内容,然后扩展所有语法:
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
}
这将阻止我使用ip4s的ipv4插值器。我希望有一个我不知道的劈刀技巧。也许在多蒂有。谢谢
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
}