By Finkelson


2015-11-01 15:42:13 8 Comments

I'm trying to create a case class with multiple constructors:

object App {
  def main(args: Array[String]) {
    val a = Something("abc", 100500, _ % 2 == 0)
    val b = Something(true, 10, 20)   
    println(s"$a - $b")
  }
}

case class Something(s: String, n: Int, p: Int => Boolean) {
  /*
  Additional constructor -- Wrong way! -- it is imposible to invoke it outside the class
  def this(b: Boolean, x: Int, y: Int) {
    this("", 0, (i: Int) => i % x + y == 0)
  }
  */
}

So far my code doesn't work:

Error:(10, 23) type mismatch;
 found   : Boolean(true)
 required: String
    val b = Something(true, 10, 20)
                      ^

To fix it I need to create a companion object to hold an apply function which represents a new constructor for Something class:

object Something {    
  def apply(b: Boolean, x: Int, y: Int) = {
    new Something(if (b) "" else "default", 0, _ => {
      (x + y) % 2 == 0
    })
  }   
}

It is inconvenient. Maybe there is some other way to place multiple constructors into the case class?

1 comments

@Archeg 2015-11-01 15:55:53

Actually it works, but you have to use new as auxiliary constructors do not have apply generated for case class:

case class Something(s: String, n: Int, p: Int => Boolean) {
  def this(b: Boolean, x: Int, y: Int) {
    this("", 0, (i: Int) => i % x + y == 0)
  }
}

new Something(true, 5, 5) // Works

If you want Something(true, 5, 5) to work, you need to create companion object as you said. I think this is because otherwise case class won't be able to work with pattern matching as it is now, or it would have been much more complicated. And notice that pattern matching won't work in this case

Also remember that case class supports default constructors like case class Something(s: String = "default") this might help you, but it does not fix your example unfortunately

Related Questions

Sponsored Content

16 Answered Questions

[SOLVED] What is the difference between Scala's case class and class?

2 Answered Questions

2 Answered Questions

[SOLVED] Modify one value in a Scala class constructor in a concise way

  • 2017-02-19 21:11:07
  • Raymond Chenon
  • 1059 View
  • 1 Score
  • 2 Answer
  • Tags:   scala case-class

0 Answered Questions

Invoking reflected case class constructor in Scala, Part 2

  • 2015-03-10 23:05:37
  • Greg
  • 50 View
  • 1 Score
  • 0 Answer
  • Tags:   scala reflection

2 Answered Questions

[SOLVED] Pre-process parameters of a case class constructor without repeating the argument list

  • 2014-09-16 11:42:58
  • sscarduzio
  • 782 View
  • 4 Score
  • 2 Answer
  • Tags:   scala

1 Answered Questions

[SOLVED] Case class for value object visibility?

1 Answered Questions

[SOLVED] Can I do structural refinement over a constructor?

  • 2013-11-13 05:35:40
  • Julkson
  • 98 View
  • 0 Score
  • 1 Answer
  • Tags:   scala

1 Answered Questions

1 Answered Questions

[SOLVED] How do I kill a RemoteActor?

  • 2010-07-25 16:52:57
  • Willem
  • 371 View
  • 4 Score
  • 1 Answer
  • Tags:   scala actor

2 Answered Questions

[SOLVED] Simple Scala syntax - trying to define "==" operator - what am I missing?

  • 2010-04-21 01:42:37
  • Alex R
  • 864 View
  • 8 Score
  • 2 Answer
  • Tags:   scala scala-2.8

Sponsored Content