scalaimplicitscala-3

Recursive value needs type error in Scala 3 implicit parameter


I'm trying to compile the following code in Scala 3 (worked on Scala 2.13):

import scala.concurrent.duration._

@main
def main(): Unit = {
  case class AAA(d: FiniteDuration)

  val duration1 = 5.seconds
  implicit val what = AAA(duration1)
}

But getting the following error:

Recursive value duration1 needs type implicit val what = AAA(duration1)

Making the parameter non implicit fixed the error. Why?


Solution

  • Try

    import scala.concurrent.duration._
    
    @main
    def main(): Unit = {
      case class AAA(d: FiniteDuration)
    
      val duration1 = 5.seconds
      implicit val what: AAA = AAA(duration1)
    }
    

    or

    import scala.concurrent.duration._
    
    @main
    def main(): Unit = {
      case class AAA(d: FiniteDuration)
    
      val duration1 = 5.seconds
      given AAA = AAA(duration1)
    }
    

    As a general guideline: always explicitly ascribe the type of all givens / implicits. It's an assignment for a compile-time type-level computation, it's better to specify what type you are assigning the given value to.