带有可选参数的重载方法

gla*_*kou 0 scala

有没有更好的方法来重写这些重载方法来避免这个double definition问题?

def test(a: Option[Int]) {
  println(a)
}

def test(a: Option[String]) {
  println(a)
}

test(Some(1))

test(Some("1"))
Run Code Online (Sandbox Code Playgroud)

示例 -> https://scastie.scala-lang.org/3V57pKeATFSmMNnDV2xBxA

gat*_*ear 5

使用多态方法

def test[T](a: Option[T]): Unit = {
    println(a)
  }
  
test(Some(1))
test(Some("1"))
Run Code Online (Sandbox Code Playgroud)