Scala-处理;方法的多个重载备选方案。。。“定义默认参数”;

Scala-处理;方法的多个重载备选方案。。。“定义默认参数”;,scala,methods,compiler-errors,overloading,Scala,Methods,Compiler Errors,Overloading,假设我有这样一个设置: sealed trait Annotation { def notes : Seq[String] } trait Something extends Annotation{ //do something funny } case class A(val i:Int)(val notes:Seq[String] = Nil) extends Something object A{ def apply(a:A)(notes:Seq[String]

假设我有这样一个设置:

sealed trait Annotation {
    def notes : Seq[String]
}

trait Something extends Annotation{
    //do something funny
}

case class A(val i:Int)(val notes:Seq[String] = Nil) extends Something
object A{
    def apply(a:A)(notes:Seq[String] = Nil):A = A(a.i)(notes)
}

case class B(val b:Boolean)(val notes:Seq[String] = Nil) extends Something
object B{
    def apply(b:B)(notes:Seq[String] = Nil):B = B(b.b)(notes)
}

case class C(val s:String)(val notes:Seq[String] = Nil) extends Something
object C{
    def apply(c:C)(notes:Seq[String] = Nil) :C = C(c.s)(notes)
}
尝试这样做会导致

Main.scala:10: error: in object A, multiple overloaded alternatives of method apply define
default arguments.
object A{
       ^

Main.scala:15: error: in object B, multiple overloaded alternatives of method apply define
default arguments.
object B{
       ^

Main.scala:20: error: in object C, multiple overloaded alternatives of method apply define
default arguments.
object C{
       ^
three errors found
我读过,所以我至少知道为什么会发生这种情况,但我不知道我应该如何解决这个问题


当然,一种可能是,在不存储任何注释时,简单地忽略默认值并强制客户机提供Nil,但是有更好的解决方案吗

我的第一个猜测是简单地将默认参数显式化:

case class A(i: Int)(val notes: Seq[String]) extends Something
object A {
  def apply(i: Int): A = new A(i)(Nil)
  def apply(a: A)(notes: Seq[String]): A = new A(a.i)(notes)
  def apply(a: A): A = new A(a.i)(Nil)
}
但是,现在,由于使用curry,您只需要在作用域中有一个同名的函数
Int=>a
Int=>Seq[String]=>a
(与
a=>a
类似)


如果不使用curry,可以手动定义重载方法:

case class B(b: Boolean, notes: Seq[String]) extends Something

object B {
  def apply(b: Boolean): B = B(b, Nil)
  def apply(b: B, notes: Seq[String] = Nil): B = B(b.b, notes)
}
class C(val s:String, val notes:Seq[String] = Nil) extends Something {

  override def toString = s"C($s)"

  override def equals(o: Any) = o match {
    case C(`s`) => true
    case _ => false
  }

  override def hashCode = s.hashCode
}

object C{
  def apply(s: String, notes: Seq[String]) = new C(s, notes)
  def apply(s: String): C = C(s, Nil)
  def apply(c:C, notes:Seq[String] = Nil): C = C(c.s, notes)
  def unapply(c: C): Option[String] = Some(c.s)
}
但是,由于
notes
现在是与
b
相同的参数列表的一部分,因此案例类方法(如
toString
)的行为发生了更改

println(B(true))                  // B(true,List())
println(B(true, List("hello")))   // B(true,List(hello))
println(B(B(false)))              // B(false,List())

最后,为了更接近地模拟原始行为,您可以实现自己的
equals
hashCode
toString
不应用的方法:

case class B(b: Boolean, notes: Seq[String]) extends Something

object B {
  def apply(b: Boolean): B = B(b, Nil)
  def apply(b: B, notes: Seq[String] = Nil): B = B(b.b, notes)
}
class C(val s:String, val notes:Seq[String] = Nil) extends Something {

  override def toString = s"C($s)"

  override def equals(o: Any) = o match {
    case C(`s`) => true
    case _ => false
  }

  override def hashCode = s.hashCode
}

object C{
  def apply(s: String, notes: Seq[String]) = new C(s, notes)
  def apply(s: String): C = C(s, Nil)
  def apply(c:C, notes:Seq[String] = Nil): C = C(c.s, notes)
  def unapply(c: C): Option[String] = Some(c.s)
}
例如:

val c1 = C("hello")
val c2 = C("hello", List("world"))
println(c1)                 // C(hello)
println(c2)                 // C(hello)
println(c1 == c2)           // true
c1 match {                  // hello
  case C(n) => println(n)
  case _ =>
}

是的,关于相关问题,你是对的。或者,您可以创建不同于“应用”的方法。e、 g.
def with notes(notes:Seq[String]=Nil)
如果我们假设我应该使用apply?