Deprecated parametrized enum overriding

Hey,
I’m building a parser in Scala 3 for some log files and I’d like to create some enums that represent the different handled tokens. Here are the enums :

enum RawLogToken(val line: Int):
    case TimestampToken(ts: Timestamp, override val line: Int) extends RawLogToken(line)
    case TextBlockToken(text: String, override val line: Int)  extends RawLogToken(line)
    case LogLineToken(text: String, override val line: Int)    extends RawLogToken(line)
    case UnknownToken(text: String, override val line: Int)    extends RawLogToken(line)

As you can see, this enum has the line number from the read file. This compiles fine.
But in the next step of the parsing pipeline, I try to separate the different values into different sub-enums:

sealed trait LogLine(val line: Int)

enum HeaderLine(line: Int) extends LogLine(line):
    case Rank(i: Int, override val line: Int) extends HeaderLine(line)
    /* a lot of other cases */

enum IterationLine(line: Int) extends LogLine(line):
    case BeginIteration(ts: Timestamp, number: Int, override val line: Int) extends IterationLine(line)
    /* a lot of other cases */

enum DismissLine(line: Int) extends LogLine(line): // lines we don't really care about
    case ConnectionSetup(override val line: Int) extends DismissLine(line)
    /* a lot of other cases */

This gives warning that the ‘override val’ in the cases is a deprecated way of doing things.
What is the recommended way of solving this ? Do I have to fall back to sealed traits and case classes instead of enums ?

Best,
MRandl

1 Like

The precise warning is as follows :

overriding val parameter value line in trait LogLine is deprecated, will be illegal in a future version
[warn] -- Deprecation Warning: /Users/mathis/Documents/proj/dp-logs-analyzer/src/main/scala/LineGrouper.scala:30:66 
[warn] 30 |case class UnknownLine(offenders: List[RawLogToken], override val line: Int) extends LogLine(line)
1 Like

I saw you got the help you needed on Discord. Mind recording that help here, to help others and avoid duplicated effort?

1 Like

yes. the final setup looks like this :

sealed trait LogLine:
    def line: Int

enum HeaderLine extends LogLine:
    case Rank(i: Int, line: Int)
    //blabla

enum IterationLine extends LogLine:
    case BeginIteration(ts: Timestamp, number: Int, line: Int)
    //blabla

Turning the val into a def in the parent trait, and letting the case define their own ‘line’ arg instead of mentioning it in the intermediate traits removed the issue.

2 Likes