600000007

プログラムと折り紙のブログです。

case class B extends case class A はダメ?

case classを継承したcase classを書いていたらwarningがでました。

case class A (x:Int)
case class B (override val x:Int) extends A(x)

コンパイルは通るのですが・・・

[warn] there were 1 deprecation warnings; re-run with -deprecation for details

deprecation(廃止予定)なコードがあるよ、とのことです。
詳細を調べる為に以下のオプションをbuild.sbtに追加しました。

scalacOptions += "-deprecation"

scalacOptions += "-Xmigration"

これで、もっと詳細なdeprecation情報が出力されます。

case class `class B has case ancestor `class A'.  Case-to-case inheritance has potentially dangerous bugs which are unlikely to be fixed.  You are strongly encouraged to instead use extractors to pattern match on non-leaf nodes.

頑張って意訳してみました。

case class Bはcase class Aを継承しているよね。
でもcaseからcaseの継承には、直される見込みが無い潜在的なバグがあります。
なのでその代わりに非リーフノード上の抽出子によるパターンマッチを使うのを推奨するよ!

ふーん。
とにかくcase to caseの継承はやめなさいって事ですね。
今回はcase class A内部でごにょごにょ実装を書きたかっただけだったので、Aをtraitに変えて対応しました。

trait A { val x:Int }
case class B (x:Int) extends A

この質問を参考にしました。

しかし潜在的なバグって何だろう・・・