2
object TablesReading {
  def read(region: String)(date: Option[String] = None): String = {
    region
  }

  def read(date: Option[String] = None): String = {
    "all"
  }
}

This throws the following error (scala version 2.13):

in object TablesReading, multiple overloaded alternatives of method read define default arguments.

I read Why does the Scala compiler disallow overloaded methods with default arguments? and https://docs.scala-lang.org/sips/named-and-default-arguments.html and learned that two overloads that have default values on the same parameter position would collide. But in this example, the parameter positions that have default values are different in the two overloads - wouldn't they generate two distinct functions like read$default$2 and read$default$1? What am I missing?

largecats
  • 195
  • 1
  • 14
  • 4
    You are not missing anything. It was just decided, that allowing this in some cases and not in others would add too much complexity, so the rule is simple: if a method is overloaded, only one version is allowed to have default arguments. – Dima Mar 10 '21 at 11:13
  • @Dima Thanks for the clarifications. Could you post the comment as an answer so that I can accept it? Thank you! – largecats Mar 10 '21 at 14:14

0 Answers0