重载方法应该有替代方案:

overloaded method should with alternatives:

我正在尝试在 intellij 中执行以下代码并出现错误。请帮忙

代码

package retcalc

object RetCalc extends App {
  def futureCapital(interestRate: Double, nbOfMonths: Int, netIncome: Int, currentExpenses:
  Int, initialCapital: Double): Double = {
    val monthlySavings = netIncome - currentExpenses

    def nextCapital(accumulated: Double, month: Int): Double =
      accumulated * (1 + interestRate) + monthlySavings

    (0 until nbOfMonths).foldLeft(initialCapital)(nextCapital)
  }
}

测试用例

package retcalc

import org.scalactic.{Equality, TolerantNumerics, TypeCheckedTripleEquals}
import org.scalatest.{Matchers, WordSpec}

class RetCalcSpec extends WordSpec with Matchers with TypeCheckedTripleEquals {

  implicit val doubleEquality: Equality[Double] =
    TolerantNumerics.tolerantDoubleEquality(0.0001)

  "RetCalc.futureCapital" should {
    "calculate the amount of savings I will have in n months" in {
      val actual = RetCalc.futureCapital(
        interestRate = 0.04 / 12, nbOfMonths = 25 * 12,
        netIncome = 3000, currentExpenses = 2000,
        initialCapital = 10000)
      val expected = 541267.1990
      actual should ===(expected)
    }
  }
}

错误

overloaded method should with alternatives:
  (inv: org.scalautils.TripleEqualsInvocationOnInterval[Double])Unit <and>
  [U](inv: org.scalautils.TripleEqualsInvocation[U])(implicit constraint: org.scalautils.EqualityConstraint[Double,U]): Unit <and>
  (notWord: RetCalcSpec.this.NotWord)RetCalcSpec.this.ResultOfNotWordForNumeric[Double] <and>
  (rightMatcherGen1: RetCalcSpec.this.MatcherGen1[Double,org.scalautils.Equality])(implicit equality: org.scalautils.Equality[Double]): Unit <and>
  (rightMatcherX3: org.scalatest.matchers.Matcher[Double])Unit
 cannot be applied to (org.scalactic.TripleEqualsSupport.TripleEqualsInvocation[Double])
      actual should ===(expected)

----scalaVersion := "2.13.6"----

我认为 === 在这里不是可用的匹配器。

最后一行应为:

actual shouldBe expected

with below I am unable to import Matchers and WordSpec

Scala 3.2.x 经历了模块化过程,因此导入发生了一些变化,

libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.9" % "test"

尝试以下导入

import org.scalactic.{Equality, TolerantNumerics, TypeCheckedTripleEquals}
import org.scalatest.wordspec.AnyWordSpec
import org.scalatest.matchers.should.Matchers

class RetCalcSpec extends AnyWordSpec with Matchers with TypeCheckedTripleEquals { ...

我也无法用 ScalaTest 3.1.0 重现该问题,因此如果您希望保留当前的导入,您可以尝试模块化前的版本。