Range.Partial [Double,NumericRange [Double]]隐式转换

时间:2015-11-11 06:03:33

标签: scala

我对隐式转换有疑问......

1 to 4: Range[Int] -> Queue[Int]转换效果很好

但是1.0 to 4.0 -> Queue[Double]不起作用......

我的代码问题是什么?

=========================================

import scala.collection.immutable._

sealed trait XS[A]{
  def print: Unit = this match {
    case RXS(q) => println(q)
    case LXS(msg) => println(msg)
  }
}

case class RXS[A](q: Queue[A]) extends XS[A]
case class LXS[A](msg: String) extends XS[A]

object XS {

  implicit def seqToQueue[A](seq: Seq[A]): Queue[A] = {
    seq.foldRight(Queue[A]())((a, acc) => a +: acc)
  }

  def apply[A](seq: Seq[A]): XS[A] = {
    RXS(seq)
  }
}

object XSTest extends App {

  val ran = 1 to 9 //Range
  val ob = XS(ran)
  ob.print

  /*val ran2 = 1.0 to 9.0 //Range.Partial[Double, NumericRange[Double]]
  val ob2 = XS(ran2)
  ob2.print*/
}

1 个答案:

答案 0 :(得分:7)

不幸的是1.0 to 9.0不会产生Seq[Double]的子类型 它是Range.Partial,期望完全指定额外的step参数。

尝试使用

XS(1.0 to 9.0 by 1.0)