Extending a scala class with less ceremony

matanster

I am probably doing something wrong. When I extend a class, I specify the mapping between the extendee's constructor and the extended class constructor:

class Base(one: String, two: String)
case class Extended(one: String, two: String, three: String) extends Base(one, two)

How can I instead suffice with something like any of the following, thus implying a "default" mapping?

class Base(one: String, two: String) 
case class Extended(one: String, two: String, three: String) extends Base

class Base(one: String, two: String) 
case class Extended(three: String) extends Base

I am probably missing the cleaner way of just adding a parameter without all that ceremony. Or should I be using a trait rather than subclassing, for such simple thing....

misberner

All the parameters of a case class generated apply method have to be specified in the case class declaration, so your second proposal cannot work. The first one can be accomplished if Base is abstract, using abstract vals:

abstract class Base {
  // no initializers!
  val one : String
  val two : String

  def print { println("Base(%s, %s)" format (one, two)) }
}
// Note: constructor arguments of case classes are vals!
case class Extended(one: String, two: String, three: String) extends Base
...
Extended("foo", "bar", "baz").print // prints 'Base(foo, bar)'

However, the names need to match exactly.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Parsing Json in scala with extending java class

From Dev

Scala: how to refer to the type of an extending class in a superclass?

From Dev

Scala case class extending Product with Serializable

From Dev

Scala conflict of types when extending a class

From Dev

Extending a Java class in Scala while keeping the field accessible

From Dev

Extending a Java class in Scala while keeping the field accessible

From Dev

Extending a class

From Dev

Unambiguous DateTime representation with NodaTime -- can this be done with less ceremony?

From Dev

Extending vs not extending a class in PHP

From Dev

Generically extending Scala collections

From Dev

Scala enum extending trait

From Dev

Scala enum extending trait

From Dev

Scala Wrapper class by extending Component and with the SequentialContainer.Wrapper trait, do I have the correct understanding of traits?

From Dev

Scala Wrapper class by extending Component and with the SequentialContainer.Wrapper trait, do I have the correct understanding of traits?

From Dev

Extending pygame sprite class

From Dev

Extending class in a singleton object

From Dev

Error when extending class

From Dev

Extending a nested class method

From Dev

Extending class at runtime

From Dev

Reverse Extending a Class

From Dev

Extending TCPDF class in Laravel

From Dev

Subclass not extending abstract class

From Dev

Extending the Validator class in Laravel

From Dev

extending a class with special overload

From Dev

Rails extending Date class

From Dev

Extending QFrame in custom class

From Dev

Extending an Abstract Class

From Dev

Extending a class at runtime

From Dev

extending a class with a generic T

Related Related

HotTag

Archive