As Scala automatically returns the last expression of a block, you just don't need return
.
As the return
exit immediately the function, it increase the complexity of the function and may cause some hard-to-understand behaviors.
Moreover, it has some unwanted behaviors, for example inside an anonymous function or a lambda. For example, this code compile fine:
def asStrings(list: Seq[Int]): Seq[String] =
list.map(i => i.toString)
but not this one:
def asStrings(list: Seq[Int]): Seq[String] =
list.map(i => return i.toString)
// Error:(2, 32) type mismatch;
// found : String
// required: Seq[String]
// list.map(i => return i.toString)
neither that one:
def asStrings(list: Seq[Int]): String =
list.map(i => return i.toString)
// Error:(2, 17) type mismatch;
// found : Seq[Nothing]
// required: String
// list.map(i => return i.toString)
and not even this one:
def asStrings(list: Seq[Int]): Seq[Nothing] =
list.map(i => return i.toString)
// Error:(2, 32) type mismatch;
// found : String
// required: Seq[Nothing]
// list.map(i => return i.toString)
This rule is also present in other best practice guides: