142

How do you provide overloaded constructors in Scala?

Landon Kuhn
  • 76,451
  • 45
  • 104
  • 130

5 Answers5

193

It's worth explicitly mentioning that Auxiliary Constructors in Scala must either call the primary constructor (as in landon9720's) answer, or another auxiliary constructor from the same class, as their first action. They cannot simply call the superclass's constructor explicitly or implicitly as they can in Java. This ensures that the primary constructor is the sole point of entry to the class.

class Foo(x: Int, y: Int, z: String) {  
  // default y parameter to 0  
  def this(x: Int, z: String) = this(x, 0, z)   
  // default x & y parameters to 0
  // calls previous auxiliary constructor which calls the primary constructor  
  def this(z: String) = this(0, z);   
}
Jon McAuliffe
  • 3,137
  • 1
  • 20
  • 10
  • @Jon McAuliffe: Bad example? Without second and third constructors, the user still can call `new Foo(x=2,z=4)` and `new Foo(z=5)` if you change your first line to `class Foo(x: Int = 0, y: Int = 0, z: String) {` – user2987828 Apr 04 '14 at 08:19
  • Named/Default arguments didn't arrive until Scala 2.8. – Jon McAuliffe Apr 14 '14 at 21:25
  • 2
    It would be worth to mention how to use an overload constructor. It's not trivial that the `new` keyword is necessary even for case classes. – Readren Apr 19 '18 at 04:17
33
 class Foo(x: Int, y: Int) {
     def this(x: Int) = this(x, 0) // default y parameter to 0
 }
Landon Kuhn
  • 76,451
  • 45
  • 104
  • 130
16

As of Scala 2.8.0 you can also have default values for contructor- and method parameters. Like this

scala> class Foo(x:Int, y:Int = 0, z:Int=0) {                           
     | override def toString() = { "Foo(" + x + ", " + y + ", " + z + ")" }
     | }
defined class Foo

scala> new Foo(1, 2, 3)                                                    
res0: Foo = Foo(1, 2, 3)

scala> new Foo(4)                                                          
res1: Foo = Foo(4, 0, 0)

Parameters with default values must come after the ones with no default values in the parameter list.

Jörgen Lundberg
  • 1,799
  • 3
  • 22
  • 31
  • 3
    This does not work for non-trivial defaults though. so `class Foo(val x:Int, y:Int=2*x)` does not work. – subsub Feb 06 '12 at 09:57
  • @Jörgen Lundberg: you wrote Parameters with default values must come after the ones with no default values in the parameter list. It is wrong, `new Foo(x=2,z=4)` will print `Foo(2,0,4)`. – user2987828 Apr 04 '14 at 08:21
  • @user2987828 what I meant was you can't write new Foo(12, x=2) you must write new Foo(x=2, 12). You can write new Foo(12, y=2), then you'll get Foo(12, 2, 0) – Jörgen Lundberg Sep 26 '14 at 13:10
10

While looking at my code, I suddenly realized that I did kind of an overload a constructor. I then remembered that question and came back to give another answer:

In Scala, you can’t overload constructors, but you can do this with functions.

Also, many choose to make the apply function of a companion object a factory for the respective class.

Making this class abstract and overloading the apply function to implement-instantiate this class, you have your overloaded “constructor”:

abstract class Expectation[T] extends BooleanStatement {
    val expected: Seq[T]
    …
}

object Expectation {
    def apply[T](expd:     T ): Expectation[T] = new Expectation[T] {val expected = List(expd)}
    def apply[T](expd: Seq[T]): Expectation[T] = new Expectation[T] {val expected =      expd }

    def main(args: Array[String]): Unit = {
        val expectTrueness = Expectation(true)
        …
    }
}

Note that I explicitly define each apply to return Expectation[T], else it would return a duck-typed Expectation[T]{val expected: List[T]}.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
flying sheep
  • 8,475
  • 5
  • 56
  • 73
0

Try this

class A(x: Int, y: Int) {
  def this(x: Int) = this(x, x)
  def this() = this(1)
  override def toString() = "x=" + x + " y=" + y
  class B(a: Int, b: Int, c: String) {
    def this(str: String) = this(x, y, str)
    override def toString() =
      "x=" + x + " y=" + y + " a=" + a + " b=" + b + " c=" + c
  }
}
anish
  • 6,884
  • 13
  • 74
  • 140