Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

overload method needs result type in Scala?

Tags:

scala

Here is my code and I actually do not need any return values and types, and wondering how to handle this error?

Error of "overload method needs result type" is on this line foo (start, end, 14)

object HelloWorld {

  def foo(start: String, end: String) = {
     foo (start, end, 14)
  }

  def foo(start: String, end: String, id: Int) = {
     println("Hello, world!")
  }

  def main(args: Array[String]): Unit = {

    foo("hello", "scala")
  }
}

Corrected version of code,

object HelloWorld {

  def foo(start: String, end: String): Unit = {
     foo (start, end, 14)
  }

  def foo(start: String, end: String, id: Int): Unit = {
     println("Hello, world!")
  }

  def main(args: Array[String]): Unit = {

    foo("hello", "scala")
  }
}

thanks in advance, Lin

like image 431
Lin Ma Avatar asked Dec 19 '22 18:12

Lin Ma


1 Answers

Here is my code and I actually do not need any return values and types, and wondering how to handle this error?

You do. Scala doesn't allow you to have two foo methods without specifying return types (both Unit in this case).

like image 161
Alexey Romanov Avatar answered Jan 03 '23 15:01

Alexey Romanov