共通のスーパークラスを拡張するいくつかのケース クラスがあり、メソッドを使用してスーパークラスからフィールドにアクセスしたいと考えていますproductElement
(ベース クラスをケース クラスとして宣言しようとしましたが、ケース クラスの継承の危険性について恐ろしい警告が表示されます。それでも機能しません)。
次のような解決策を想像できます。
abstract class A(a: Int) extends Product {
def productArity = 1
def productElement(n: Int) = if (n == 0) a else throw new IndexOutOfBoundsException
}
case class B(b: Int) extends A(1) {
def productArity = super.productArity + 1
def productElement(n: Int) = if (n < super.productArity) super.productElement(n) else ....
}
しかし、あまりにも醜いので、終わらせることさえできませんでした。
誰もがより良い解決策を知っていますか?