私は現在、複数の型パラメータの1つの特性に適合するオブジェクト/クラス/特性を構成することについて不思議に思っています。 私IntStrDependent
形質のインスタンスがしなければならないことそれでは、私は同じ特性を持つが異なる型パラメータを持つ合成
trait Dependent[T]{
def observeCritereaChanged(oldValue:T, newValue:T):Unit
}
を持っているとしましょう、私は例
trait IntStrDependent extends Dependent[Int] with Dependent[String]
のために、二つの異なるタイプのパラメータに対する依存実装し、いくつかの特性を定義できるようにしたいと思います両方のタイプのobserveCritereaChanged
を定義します。
class MyDependent extends IntStrDependent {
def observeCritereaChanged(oldValue:Int, newValue:Int) = //...
def observeCritereaChanged(oldValue:String, newValue:String) = //...
}
これまでしようとしたときに私の努力は、コンパイルエラーで満たされていますIntStrDependent
形質を作成します。
scala> trait IntStrDependent extends Dependent[Int] with Dependent[String]
<console>:8: error: illegal inheritance;
self-type IntStrDependent does not conform to Dependent[Int]'s selftype Dependent[Int]
trait IntStrDependent extends Dependent[Int] with Dependent[String]
^
<console>:8: error: illegal inheritance;
self-type IntStrDependent does not conform to Dependent[String]'s selftype Dependent[String]
trait IntStrDependent extends Dependent[Int] with Dependent[String]
^
だから私の質問は:私がやろうとしている(そうであれば、どのように)、またはスカラ座がに内蔵されていませんので、これは失われた原因である何をする方法はありますこの種のことをやる?
これは私が通常やっていることです。 –
あなたがリンクしている面白い読書。それは間違いなく私の質問に答えるが、私はあなたのアプローチについていくつもりだと思う:私のアプリケーションでは、「is a」から「has a」関係に変わるべきである – Dylan