145

How do you provide overloaded constructors in Scala?

5 Answers 5

194

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);   
}
Sign up to request clarification or add additional context in comments.

3 Comments

@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) {
Named/Default arguments didn't arrive until Scala 2.8.
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.
33
 class Foo(x: Int, y: Int) {
     def this(x: Int) = this(x, 0) // default y parameter to 0
 }

Comments

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.

3 Comments

This does not work for non-trivial defaults though. so class Foo(val x:Int, y:Int=2*x) does not work.
@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 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)
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]}.

Comments

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
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.