ClassFormatError involving trait parameters in Scala 3


Say I define a trait with a parameter:

trait Foo(val num: Int)

I then extend it in a class that has an identically named parameter:

class Bar(num: Int) extends Foo(num):   
  def bar = this.num

Trying to instantiate this class with Bar(123) produces a ClassFormatError, which complains about duplicate field names. Here’s a Scastie.

If Foo was a class instead, the code would work.

Is this a known issue?

(I know that renaming and override val are possible solutions here, but it would be very nice if the code worked on traits like it works on classes.)

Thank you!

That does not seem to be a known issue. Which is strange because it’s pretty easy to run into.
I suggest that you report it here.

All right, I’ve just gone and done that. Thank you!

1 Like