I am not sure what the difference is between specifying Unit as the return type of my scala method or leaving out the return type altogether. What is the difference?
Can anyone please advise?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Implicit
Unitreturn type:Explicit
Unitreturn type:Return type inferred from the last method expression, still
Unitbecause this is the type ofprintln, but confusing:All the methods above are equivalent. I would prefer
f()because the lack of=operator after method signature alone is enough for me. Use explicit: Unitwhen you want to extra document the method. The last form is confusing and actually treated as a warning in intellij-idea.The
=operator is crucial. If it is present it means: “please return whatever the last statement returns” in method body. Obviously you cannot use this syntax forabstractmethods. If it is not,Unitis assumed.