scala> class Rectangle (Length: Int, Width: Int) {; def getLlength (): Int = println (Length); def getWidth ():Int = println (Width); }
:11: error:
You can use a structure something like this if you want to print out the values of Length and width as well as keep the return type of your methods to be Int.
class Rectangle (Length: Int, Width: Int) {
def getLlength (): Int ={ println (Length); Length}
def getWidth ():Int = {println (Width); Width}
}
what happens in scala is that the compiler treats the last line of the method always as a return statement and hence when you were just writing a println() inside your method body the Scala compiler was assuming that to be your return statement as well and in Scala return type of println() is Unit that is why you are getting this error. So if you want Int as your return type you just need to add one more line as the last line of your method which is your Int parameter Length or Width.