次の例S
の呼び出しから型パラメーターを削除する方法を探しています。apply
object Attribute {
trait Int [S] extends Attribute[S]
trait Boolean[S] extends Attribute[S] // etc.
}
sealed trait Attribute[S]
trait Attributes[S] {
protected def get(key: String): Option[Attribute[S]]
def apply[A <: Attribute[S]](key: String)
(implicit tag: reflect.ClassTag[A]): Option[A] =
get(key) match {
case Some(attr: A) => Some(attr)
case _ => None
}
}
上記の定義では、テスト ケースは次のようになります。
trait Test[S] {
def map: Attributes[S]
map[Attribute.Int[S]]("foo")
}
私がやろうとしているのは、次を許可するように定義を変更するapply
ことです:
trait Test2[S] {
def map: Attributes[S]
map[Attribute.Int]("foo") // use partially applied attribute type
}
編集:マリウスの提案とコメントをフォローアップすると、なぜ以下がまだ消去警告を生成するのですか:
import reflect.runtime.universe._
trait Attributes[S] {
protected def get(key: String): Option[Attribute[S]]
def apply[A[x] <: Attribute[x]](key: String)
(implicit tag: TypeTag[A[S]]): Option[A[S]] =
get(key) match {
case Some(attr: A[S]) => Some(attr)
case _ => None
}
}
私には、それは明らかに意味がありません。一方で、利用可能な完全なタイプのタグがA[S]
あります。一方、Attribute
が不変であるS
ように、それがなくても完全に機能するはずです。Option[Attribute[S]]
Some(attr: A[x])
x == S
編集 2 : 解決策の条件は、Attribute
型コンストラクターのパラメーターS
をメンバー フィールドに移動しないなど、特性の形状が変更されないことです。