I'm new to Scala programming and have a question on an error. I have defined a method inside a class. Below is the code (Private_Rational is the class name)
def + (that: Private_Rational): Private_Rational = { new Private_Rational(numer * that.denom + that.numer * denom, denom * that.denom) println("I'm in first plus") } The above code flashed an error (it forced me to use Unit as the return type), however when I moved the println statement above the new statement, it worked
def + (that: Private_Rational): Private_Rational = { println("I'm in first plus") //Moved println here new Private_Rational(numer * that.denom + that.numer * denom, denom * that.denom) } Can anyone please enlighten on why the compiler didn't like the first one? Appreciate your help. -Thanks.
printlnreturnsUnitthat's all.