45

I would like to check constructor arguments and refuse to construct throwing IllegalArgumentException in case the arguments set is not valid (the values don't fit in expected constraints). How to code this in Scala?

Ivan
  • 63,011
  • 101
  • 250
  • 382

2 Answers2

94

In Scala, the whole body of the class is your primary constructor, so you can add your validation logic there.

scala> class Foo(val i: Int) {
     |   if(i < 0) 
     |     throw new IllegalArgumentException("the number must be non-negative.")
     | }
defined class Foo

scala> new Foo(3)
res106: Foo = Foo@3bfdb2

scala> new Foo(-3)
java.lang.IllegalArgumentException: the number must be positive.

Scala provides a utility method require that lets you write the same thing more concisely as follows:

class Foo(val i: Int) {
  require(i >= 0, "the number must be non-negative.")
}

A better approach might be to provide a factory method that gives a scalaz.Validation[String, Foo] instead of throwing an exception. (Note: requires Scalaz)

scala> :paste
// Entering paste mode (ctrl-D to finish)

class Foo private(val i: Int)

object Foo {
  def apply(i: Int) = {
    if(i < 0)
      failure("number must be non-negative.")
    else
      success(new Foo(i))
  }
}

// Exiting paste mode, now interpreting.

defined class Foo
defined module Foo

scala> Foo(3)
res108: scalaz.Validation[java.lang.String,Foo] = Success(Foo@114b3d5)

scala> Foo(-3)
res109: scalaz.Validation[java.lang.String,Foo] = Failure(number must be non-negative.)
akku
  • 76
  • 1
  • 7
missingfaktor
  • 90,905
  • 62
  • 285
  • 365
  • 6
    Throwing exceptions in constructors is less than ideal. Whenever I need a type with restrictions on the domains of any of its constructor parameters, I make the constructor private and force instantiation through a factory that applies the constraints and either throws (via `require`, usually) or returns `Try[ConstrainedType]`. – Randall Schulz Apr 02 '14 at 16:42
  • I think you mean that the message in require should say `non-negative`. – Fernando Correia Apr 15 '16 at 14:57
19
scala> class Foo(arg: Int) {
     |   require (arg == 0)
     | }
defined class Foo

scala> new Foo(0)
res24: Foo = Foo@61ecb73c

scala> new Foo(1)
java.lang.IllegalArgumentException: requirement failed
Luigi Plinge
  • 50,650
  • 20
  • 113
  • 180
  • 2
    Nice! What a perfect answer to the question. Can you elaborate a little (or link to elaboration) on the details of how `require` works? – Dan Burton Feb 07 '12 at 03:34
  • 1
    Here's how does `require` work, @dan-burton ;-) http://pastebin.com/rJQfTCX9 It just throws `new IllegalArgumentException("requirement failed")` if the argument is false. The whole idea of using `require` instead of just throwing the exception yourselves is to make your code prettier and better suitable for static analysis (which seems not implemented yet). – Ivan Feb 07 '12 at 04:25