Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Early return from a Scala constructor

I am writing the constructor for my "main" class. The first thing it does is call a method to use commons-cli to parse the command line. If the parseOptions method returns false, an error has occurred, and the constructor should exit.

I tried writing the following code

if (!parseOptions(args)) return

but the compiler complains that I have a "Return statement outside method definition".

Short of calling System.exit(1) or inverting the boolean (and putting all of the rest of my logic inside the if statement, is there any way to return "early" from a constructor?

I suppose I could have the parseOptions method throw an IllegalArgumentException and catch that in my Main object.

Thanks.

like image 421
Ralph Avatar asked Aug 23 '10 12:08

Ralph


3 Answers

Dont try to do a early/premature return, this makes your code harder more complex, since the side effects of the return can be hard to understand. Instead use a exception to signal that something is wrong.

You can use require in the constructor. This doesn't return. But it seems like throwing an exception actually fits his situation better.

As in:

class MyTest(
private var myValue: Int ){

    require(myValue > 0) // Connected to constructor

}

defined class MyTest

scala> val x = new  MyTest(10)
x: MyTest = MyTest@49ff4282

scala> val y = new MyTest(-10)
java.lang.IllegalArgumentException: requirement failed
        at scala.Predef$.require(Predef.scala:133)
like image 155
oluies Avatar answered Nov 03 '22 19:11

oluies


is there any way to return "early" from a constructor

No. But in your case it sounds like bad design, anyway.

If the parseOptions method returns false, an error has occurred

In this case the constructor should throw an exception, not return normally.

like image 11
Alexey Romanov Avatar answered Nov 03 '22 18:11

Alexey Romanov


A constructor should always either complete fully, or abort (throw an exception). Anything else leaves your object "half constructed" and thus impossible to reason about.

If in your case, the object is valid even if parseOptions failed, then you can change the condition and continue:

if (parseOptions(args)) {
  // rest of constructor
}
like image 4
IttayD Avatar answered Nov 03 '22 18:11

IttayD