What is wrong with the Scala overload method

The code below produces a compilation error in the worksheet

 def joiner(strings:List[String], separator:String):String = strings.mkString(separator)
  def joiner(strings:List[String]):String = joiner(strings, "  ")

  joiner(List("sdsdfsd", "sdsd"))

Mistake:

Error:(12, 120) too many arguments for method joiner: (strings: List[String])String
println("joiner: " + MacroPrinter211.printGeneric({import inst$A$A._ ;def joiner(strings:List[String]):String = joiner(strings, "  ") }).replace("inst$A$A.", ""))
                                                                                                                      ^

I have an overloaded joiner method. Why does he give an error that there are too many arguments?

+4
source share
2 answers

Your code works correctly if you place it inside a class or object because the class or object can overload methods in scala.

But if you write your code in REPL, these are not methods, but functions. And functions cannot be overloaded. So you have to put them inside an object or class or use the default options suggested by @StuartMcvean

. @Travis , , . , REPL ( ) - , .

(, ), , REPL , REPL , (, , )

:paste ( , ) -

+6

scala . :

def joiner(strings:List[String], separator:String = " "): String =
  strings.mkString(separator)

joiner(List("sdsdfsd", "sdsd"))
+3

Source: https://habr.com/ru/post/1625538/


All Articles