scala - (案例)类构造函数上下文中的隐式转换

标签 scala implicit-conversion implicit

我想拥有自动伴侣类apply case 类的构造函数来为我执行隐式转换,但无法弄清楚如何这样做。我到处搜索,我能找到的最接近的答案是 this问题(我将解释为什么它不是我在下面寻找的)。

我有一个看起来像这样的案例类:

case class Container(a: Long, b: Long, c: Long)

我正在使用容器来计算某些条件适用的实例,因此我希望能够让构造函数自动将 bool 参数转换为长整型( if (boolean) 1L else 0L )。

当然,真实的案例类有很多参数,所以如果让我自己的伴生对象并重载 apply 会很乏味而且非常重复。接受 Boolean参数。此外,像下面的代码并不理想(如果它以某种方式正确实现),因为它只接受 bool 参数:
object Container {
  def apply(args: Boolean*) = {
    // doesn't REALLY work since number of arguments not enforced
    Container(args map { if (_) 1L else 0L } toArray: _*)
  }
}
val c1 = Container(1, 0, 1) // works
val c2 = Container(true, false, true) // might be workable if done correctly
val c3 = Container(true, 0, 1) // won't work

我尝试在伴随对象(如下)中添加一个隐式转换,希望它会在 Container.apply 中自动使用,但看起来这实际上并没有将隐式转换放入调用 apply 的代码的命名空间中。
object Container {
  implicit def booleanToLong(x: Boolean): Long = if (x) 1L else 0L
}

我可以使用这种骇人听闻的解决方法来解决问题:
{
  import Container.booleanToLong
  // all of these now work
  val c1 = Container(1, 0, 1)
  val c2 = Container(true, false, true)
  val c3 = Container(true, 0, 1) // works!!!
}

最大的问题是我必须导入booleanToLong进入想要创建Container的代码因此必须将它放在自己的块中以确保安全(booleanToLong 通常是不可取的)。

最后,使用本身包含隐式转换的隐式参数的解决方案不起作用,因为它需要显式覆盖 apply ,打破了不重复长参数列表和编码类型的目标。

有没有办法做到这一点,这样我每次制作 Container 时都可以免费获得隐式转换,但不是别的?还是由于某种技术限制,这是不可能的?

最佳答案

您可以使用 magnet pattern 的一种变体使这更安全一点。首先是一个类型类:

trait ToLong[A] {
  def apply(a: A): Long
}

implicit object longToLong extends ToLong[Long] {
  def apply(l: Long) = l
}

implicit object booleanToLong extends ToLong[Boolean] {
  def apply(b: Boolean) = if (b) 1L else 0L
}

现在我们只需要一个额外的构造函数:
case class Container(a: Long, b: Long, c: Long)

object Container {
  def apply[A: ToLong, B: ToLong, C: ToLong](a: A, b: B, c: C) = new Container(
    implicitly[ToLong[A]].apply(a),
    implicitly[ToLong[B]].apply(b),
    implicitly[ToLong[C]].apply(c)
  )
}

我们可以这样写:
val c1 = Container(1, 0, 1)
val c2 = Container(true, false, true)
val c3 = Container(true, 0L, 1L)

无需介绍来自 Boolean 的相当可怕的通用转换至 Long .

关于scala - (案例)类构造函数上下文中的隐式转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19017583/

相关文章:

scala - Scala中使用案例类的DSL

scala - case 类中的 var 成员会影响 case 类的相等性吗?

c - 我无法弄清楚数组名称的意义

c++ - 函数参数隐式转换失败

Java:隐式类型转换,或隐式 toString() 调用

arrays - 如何在scala中获得正确的数组哈希码?

scala - 是否可以限制编译器显示的错误数量?

c# - 具有数字类型和意外结果的显式和隐式运算符

Scala隐式方法参数消除歧义

scala - 如何将可以为 null 或数组的值隐式包装到 Scala 选项中