I was learning about scala methods and I created two code examples which are somewhat similar in nature, but confused on calling them.
Method #1
def someTestMethod = {
println("Inside a test method")
}
This resorts to a Unit
type since it does not return anything.
Method #2
def anotherTestMethod() = {
println("Inside a test method")
}
This resorts to Unit
as well, but with a curly brace ()
added.
What is the difference between the two methods, notice that if I call the first method like someTestMethod()
, the scala shell/compiler says
error: Unit does not take parameters, but works well if I call like someTestMethod
without the braces.
Also, the second method seems fool proof, in the sense that it can be called either way anotherTestMethod
or anotherTestMethod()
, why is it so?