Scala constructor overload?

Scala

Scala Problem Overview


How do you provide overloaded constructors in Scala?

Scala Solutions


Solution 1 - Scala

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

Solution 2 - Scala

 class Foo(x: Int, y: Int) {
     def this(x: Int) = this(x, 0) // default y parameter to 0
 }

Solution 3 - Scala

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.

Solution 4 - Scala

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

Solution 5 - Scala

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLandon KuhnView Question on Stackoverflow
Solution 1 - ScalaJon McAuliffeView Answer on Stackoverflow
Solution 2 - ScalaLandon KuhnView Answer on Stackoverflow
Solution 3 - ScalaJörgen LundbergView Answer on Stackoverflow
Solution 4 - Scalaflying sheepView Answer on Stackoverflow
Solution 5 - ScalaanishView Answer on Stackoverflow