Scala 3 does not enforce the implementation of abstract trait memebrs?

Why doesn’t Scala 3 enforce the implementation of abstract trait members when extending it with a case class or enum?

trait Base:def value: Int
  def value: Int

sealed trait Error extends Base

object Error:
  final case class Something() extends Error

This compiles successfully, even though there are non-implemented abstract members.

Why is this? Why do we have traits if they do not guarantee that there are concrete implementations of their abstract members in extending classes?

Does it? It actually fails to compile with all versions with

[error] class Something needs to be abstract, since def value: Int in trait Base is not defined
[error]   final case class Something() extends Error
[error]                    ^

Here’s sample using Scastie with same error Scastie - An interactive playground for Scala.

Also ensure that you actualy extend your defined Error type and not java.lang.Error. All definitions from scala and java.lang packages are available without explicit imports

1 Like

I must have corrupted my example while trying to format it here. I will provide a new example. Thank you so far for your help!

Let’s take this example:

trait Error:
    def message: Option[String]

object Error:
    trait ExceptionThrownError:
        def cause: Throwable


object Foo:
    enum FooError extends Error:
        case Generic extends FooError
        case ExceptionError extends FooError with Error.ExceptionThrownError

This indeed causes an compiler error in REPL, but not in Metals (at least not on edit-time). Is there something I miss?

Might be some issue with setup, or project was not yet imported. @tgodzik any ideas?

1 Like

The error will be shown after saving, not just after chaning the code, so maybe that was the issue?

Indeed, after reopening VSCode the error was shown. I have enabled auto-save in VSCode but not before manually saving the error showed up. Thank you agian!