Skip to content

Cannot override a val from the parent class #2051

Closed
@LPTK

Description

@LPTK

This works in Scalac, but not in Dotty:

class A[T](val x:T)
class B[T](override val x:T) extends A[T](x)

Dotty complains:

Welcome to Scala.next (pre-alpha, git-hash: 06d3f7a)  (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_65).
Type in expressions to have them evaluated.
Type :help for more information.
scala> class A[T](val x:T)
defined class A
scala> class B[T](override val x:T) extends A[T](x)
-- Error: <console> ------------------------------------------------------------
5 |class B[T](override val x:T) extends A[T](x)
  |                        ^
  |                        overriding value x in class A of type T;
  |                         method x of type => T has incompatible type

Removing the override val keywords in B makes it compile, but it's not always possible (e.g. for case classes).

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions