测试隐式转换是否可用

发布于 2024-11-02 04:13:30 字数 234 浏览 1 评论 0原文

我试图检测是否存在隐式转换,并根据它来执行一些代码。例如:

if (x can-be-converted-to SomeType)
  return something(conversion(x))
else
  return someotherthing(x)

例如,x 是一个 Int,应该转换为 RichInt。 这在 Scala 中可能吗?如果是,怎么办?

谢谢

I am trying to detect if an implicit conversion exists, and depending on it, to execute some code. For instance :

if (x can-be-converted-to SomeType)
  return something(conversion(x))
else
  return someotherthing(x)

For instance, x is an Int and should be converted to a RichInt.
Is this possible in Scala? If yes, how?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遥远的她 2024-11-09 04:13:30

正如其他人已经提到的,隐式是在编译时解决的,所以也许您应该使用类型类来解决这样的问题。这样您就可以在以后将功能扩展到其他类型。

此外,您可以只需要一个现有的隐式值,但除了默认参数之外,无法直接表示不存在隐式值。

Jean-Phiippe 使用默认参数的解决方案已经相当不错了,但是如果您定义一个可以代替隐式参数的单例,则可以消除 null 。将其设为私有,因为它实际上在其他代码中没有用处,甚至可能很危险,因为隐式转换可能会隐式发生。

private case object NoConversion extends (Any => Nothing) {
   def apply(x: Any) = sys.error("No conversion")
}

// Just for convenience so NoConversion does not escape the scope.
private def noConversion: Any => Nothing = NoConversion

// and now some convenience methods that can be safely exposed:

def canConvert[A,B]()(implicit f: A => B = noConversion) =
  (f ne NoConversion)

def tryConvert[A,B](a: A)(implicit f: A => B = noConversion): Either[A,B] = 
  if (f eq NoConversion) Left(a) else Right(f(a))

def optConvert[A,B](a: A)(implicit f: A => B = noConversion): Option[B] =
  if (f ne NoConversion) Some(f(a)) else None

As others already mentioned implicits are resolved at compile time so maybe you should rather use type classes to solve problems like this. That way you have the advantage that you can extend functionality to other types later on.

Also you can just require an existing implicit value but have no way of directly expressing non-existence of an implicit value except for the default arguments.

Jean-Phiippe's solution using a default argument is already quite good but the null could be eliminated if you define a singleton that can be put in place of the implicit parameter. Make it private because it is actually of no use in other code and can even be dangerous as implicit conversions can happen implicitly.

private case object NoConversion extends (Any => Nothing) {
   def apply(x: Any) = sys.error("No conversion")
}

// Just for convenience so NoConversion does not escape the scope.
private def noConversion: Any => Nothing = NoConversion

// and now some convenience methods that can be safely exposed:

def canConvert[A,B]()(implicit f: A => B = noConversion) =
  (f ne NoConversion)

def tryConvert[A,B](a: A)(implicit f: A => B = noConversion): Either[A,B] = 
  if (f eq NoConversion) Left(a) else Right(f(a))

def optConvert[A,B](a: A)(implicit f: A => B = noConversion): Option[B] =
  if (f ne NoConversion) Some(f(a)) else None
苏辞 2024-11-09 04:13:30

您可以尝试将其传递给需要相应隐式参数(默认为 null )的方法:

def toB[A](a: A)(implicit convertFct: A => B = null) =
  if (convertFct != null)
    convertFct(a)
  else
    someOtherThing(a)

请注意,我看起来很好奇在运行时检查这一点,因为编译器在编译时就知道是否存在这样的情况可以使用转换功能。

You can try to pass it to a method that needs the corresponding implicit parameter with a default of null:

def toB[A](a: A)(implicit convertFct: A => B = null) =
  if (convertFct != null)
    convertFct(a)
  else
    someOtherThing(a)

Note that it looks curious to me to check this at runtime, because the compiler knows at compile time whether such a conversion function is available.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文