Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

overloaded method should with alternatives:

I am trying to execute the below code in intellij and getting error. Please help

code

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)
  }
}

Test Case

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)
    }
  }
}

Error

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"----

like image 265
Akhil Avatar asked Oct 19 '25 09:10

Akhil


1 Answers

with below I am unable to import Matchers and WordSpec

Scala 3.2.x went through modularisation process so imports changed a bit, so with

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

try the following imports

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 { ...

I was also not able to replicate the issue with ScalaTest 3.1.0, so you could try that pre-modularisation version if you wish to keep the imports you have currently.

like image 144
Mario Galic Avatar answered Oct 21 '25 23:10

Mario Galic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!