This page is no longer maintained — Please continue to the home page at www.scala-lang.org

What is the state of case class inheritance

No replies
Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.

Hi,

I understand case class subclassing is deprecated because of some problems handling it. i just read this sentence in the newly updated named-arguments SID:

"The copy method is only added to a case class if no member named ”copy” already exists in the class or in one of its parents. This implies that when a case
class extends another case class, there will only be one copy method, namely the one from the lowest case class in the hierarchy."

which no doubt speaks of subclassing case classes.

is it possible to plea against the deprecation of case class subclassing?

imagine this very simple scenario:

case class Span( start: Long, stop: Long ) {
// [...]
}

trait Stake {
val span: Span
def replaceStart( newStart: Long ) : this.type
def replaceStop( newStop: Long ) : this.type
}

case class Region( name: String, span: Span ) {
def replaceStart( newStart: Long ) : this.type ) =
copy( span = new Span( newStart, span.stop ))

def replaceStop( newStop: Long ) : this.type =
copy( span = new Span( span.start, newStop ))
}

case class AudioRegion( override name: String, override span: Span, file: java.io.File, fileOffset: Long )
extends Region {
def replaceStart( newStart: Long ) : this.type =
copy( span = new Span( newStart, span.stop ), fileOffset = fileOffset + newStart - span.start )

def replaceStop( newStop: Long ) : this.type =
copy( span = new Span( span.start, newStop ))
}

(this doesn't compile for several reasons, one being a problem i have with 'this.type', but you get the idea).

this obviously looses the add-ons of Scala 2.8 like the copy method when prohibiting case class subclassing.

or am i missing something?

thanks, -sciss-

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland