既存のメソッドの名前Ext
を変更すると同時に新しいメソッドを定義するという名前のトレイトを定義したいと思います。トレイトは、ケースクラスを拡張するために使用されます。私の現在の解決策はどういうわけかハッキーに見えます:equals
equalsByAttributes
equals
case class A(id: Int) extends Ext
trait Ext { p: Product =>
// new implementation
override def equals(obj: Any) = obj match {
case that: AnyRef => this eq that
case _ => false
}
// reimplementation of old equals implementation
def equalsByAttributes(obj: Any) = obj match {
case that: Product =>
if (this.getClass.isAssignableFrom(that.getClass) || that.getClass.isAssignableFrom(this.getClass))
p.productIterator.toList == that.productIterator.toList
else
false
case _ => false
}
}
このメソッドの再実装を回避できるように、A
のequals
メソッドを直接参照する方法があるのだろうか?equalsByAttributes
2012年7月12日編集
スーパー実装を参照するためのソリューションがあるので、トレイトによって拡張される基本クラス/トレイトの特定の実装にアクセスするsuper.METHOD_NAME
など、同様の構文が必要だと思いました。これにより、私のトレイトは次のようになります。overridden.METHOD_NAME
Ext
trait Ext { p: Product =>
override def equals(obj: Any) = ...
def equalsByAttributes(obj: Any) = overridden.equals(obj)
}