1

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?

1
  • What do you mean by "Making the parameter non implicit" - what "parameter"? Commented Jan 16, 2024 at 18:31

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you, took your advice further and replaced "implicit val what = AAA(duration1)" with "given AAA(duration1)" and the error is gone. still wonder why implicit val with no explicit type declaration gave the cryptic compiler error.
from the migration guide: "The type of implicit definitions (val or def) needs to be given explicitly in Scala 3. They cannot be inferred anymore." I wonder just why the compiler error is so not related.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.