Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test arguments to a case class constructor?

I'd like to test the arguments to my case class constructor and throw an exception if they fail certain tests. The compiler complained when I tried to write my own apply method (Multiple 'apply' methods.

I suppose I could make it a non-case class, and do the apply/unapply constructor field stuff myself, but I had hoped not to.

Thanks

like image 282
Jim Avatar asked Sep 10 '10 19:09

Jim


1 Answers

case class Picky(i: Int, s: String) {
  require(i % 2 == 0, "i must be even")
  require(s.length < 50, "s length must be less than 50 characters")

  // ...
}
like image 161
Randall Schulz Avatar answered Oct 31 '22 10:10

Randall Schulz