【问题标题】:Upgraded to scala 2.11 from 2.9: Type argument Any does not conform to type parameter bounds T <: Comparable从 2.9 升级到 scala 2.11:类型参数 Any 不符合类型参数边界 T <: Comparable
【发布时间】:2020-07-04 04:17:54
【问题描述】:

我了解错误消息的含义,但不确定我需要在哪里添加某种类型的界限。

java 整数值不继承自 Comparable 吗?不确定我是否专注于这里的正确区域。

类型参数 [Any] 不符合方法 comp 的类型参数 界限 [T <: comparable java.lang.integer.valueof succ comp>

def inclusive[T <: Comparable[T]](
    start: T,
    finish: T,
    succ: (T) => T,
    comp: (T, T) => Int
): Seq[T] = {
  val ret = new ArrayBuffer[T]
  var value: T = start
  while (comp(value, finish) <= 0) {
    ret += value
    value = succ(value)
  }
  ret.toSeq
}

private def comp[T <: Comparable[T]](a: T, b: T) = {
  if (a != null)
    a.compareTo(b)
  else if (b == null) 0
  else -1
}

private def compInt(a: Int, b: Int) = {
  a - b
}

def inclusive[T <: Comparable[T]](
    start: T,
    finish: T,
    succ: (T) => T
): Seq[T] = {
  inclusive(start, finish, succ, comp[T])
}

def inclusive(start: util.Date, finish: util.Date): Seq[util.Date] = {
  inclusive(start, finish, succDate, comp[Date])
}

def inclusive(start: String, finish: String): Seq[String] = {
  inclusive(start, finish, succString, comp[String])
}

private def succ(x: java.lang.Integer): java.lang.Integer = x + 1
private def comp(
    x: java.lang.Integer,
    y: java.lang.Integer
): java.lang.Integer = x - y

def inclusive(start: Int, finish: Int): Seq[Int] = {
  inclusive(
    java.lang.Integer.valueOf(start),
    java.lang.Integer.valueOf(finish),
    succ,
    comp
  ).map(_.toInt)
}

【问题讨论】:

  • 您可能想改用内置的Ordering - typeclass。 - 此外,可变性和空值之类的东西在 Scala 中不是惯用的
  • @LuisMiguelMejíaSuárez 同意,一旦我在 scala 2.12.x 上运行它,我将重构它:)。这段代码是一个java开发者多年前写的!

标签: scala


【解决方案1】:

这是因为您的 comp 参数是一个函数,它返回 Int 而不是像您的其他方法一样返回 java.lang.Integer。当您添加显式类型参数时,问题变得清晰:

inclusive[java.lang.Integer](
    java.lang.Integer.valueOf(start),
    java.lang.Integer.valueOf(finish),
    succ _,
    comp _
  )

错误:

overloaded method inclusive with alternatives:
  (start: Integer,finish: Integer,succ: Integer => Integer)Seq[Integer] 
  (start: Integer,finish: Integer,succ: Integer => Integer,comp: (Integer, Integer) => Int)Seq[Integer]
 cannot be applied to (Integer, Integer, Integer => Integer, (Integer, Integer) => Integer)

而不是返回Intcomp 版本

def comp[T <: Comparable[T]](a: T, b: T) //: Int (inferred)

它选择带有这个签名的comp版本,它返回一个java.lang.Integer,导致类型不匹配:

def comp(x: java.lang.Integer, y: java.lang.Integer): java.lang.Integer

您可以通过在对comp 的调用中添加类型参数来解决此问题,以获得返回Int 的版本:

def inclusive(start: Int, finish: Int): Seq[Int] = {
  inclusive(
    java.lang.Integer.valueOf(start),
    java.lang.Integer.valueOf(finish),
    succ _,
    comp[java.lang.Integer] _
  ).map(_.toInt)
}

或将您的第一个 inclusive 函数的签名更改为:

def inclusive[T <: Comparable[T]](
    start: T,
    finish: T,
    succ: (T) => T,
    comp: (T, T) => java.lang.Integer <- Used to be Int
): Seq[T]

您也许可以将所有这些变成这样:

import scala.math.Ordering.Implicits._

def inclusive[T](
    start: T,
    finish: T,
    succ: (T) => T
)(implicit ord: Ordering[T]): Seq[T] =
  LazyList.iterate(start)(succ).takeWhile(_ < finish)

def inclusive(start: Int, finish: Int): Seq[Int] = {
  inclusive[Int](start, finish, (_ + 1))
}

【讨论】:

    猜你喜欢
    • 2018-12-16
    • 2020-07-20
    • 2019-09-30
    • 2019-12-18
    • 1970-01-01
    • 2019-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多