2016-10-21 3 views
0

:ToolでScala - 複雑なジェネリックを使ってサブクラスと別のクラスの情報を取得する - コンパイルされませんか?私の周りのフレームワークを使用していますScalaでプログラム書いてい

trait Tool[T <: Tool[T, U], U <: Settings[T]] { 
    // members here 
    def createSettingsFrom(settingsWithStringNames: Map[String, _]): U 
} 

trait Settings[T <: Tool[T, _ <: Settings[T]] 

を、Tはサブクラスであり、そしてUはそれのための情報を運ぶクラスです。各ツールは、パラメータ付きの一種のコマンドと見なすことができ、それらのパラメータはそれぞれのカスタムです。

私もその「情報担体」と一緒に、それを拡張するクラスを持っている:

object Cleanup extends Tool[Cleanup, CleanupSettings] { 
    override def createSettingsFrom(settings: Map[String, _]): CleanupSettings 
    = CleanupSettings(
     settings.get("attribute1").asInstanceOf[Int] 
     settings.get("attribute2").asInstanceOf[String]) 
} 

case class CleanupSettings extends Settings[Cleanup](
    //attribute1: Int, 
    //attribute2: String 
    //more attributes) 

私はこれらのクラスをコンパイルしようとすると、私は、次のスタックトレースを取得:

Information:21/10/16 03:20 - Compilation completed with 2 errors and 0 warnings in 3s 200ms /project_folder/src/main/scala/io/oreville/maptools/operations/cleanup/Cleanup.scala Error:(17, 24) type arguments [package.tools.operations.cleanup.Cleanup,package.tools.operations.cleanup.CleanupSettings] do not conform to trait ConfigurableTool's type parameter bounds [T <: package.tools.ConfigurableTool[T,U],U <: package.tools.params.Settings[T]] object Cleanup extends ConfigurableTool[Cleanup, CleanupSettings] { ^ /project_folder/src/main/scala/io/oreville/maptools/operations/cleanup/CleanupSettings.scala Error:(11, 11) type arguments [package.tools.operations.cleanup.Cleanup] do not conform to trait Settings's type parameter bounds [T <: package.tools.Tool[T, _ <: package.tools.params.Settings[T]]] extends Settings[Cleanup] ^

私はまた、いくつかの特別な機能を備えたToolの拡張機能であるConfigurableToolを持っています。そのため、まったく同じ汎用シグネチャがあり、ちょうどextends Tool[T, U]です。

+と - の組み合わせを自分のジェネリックに追加するなど、複数の問題を解決しようとしましたが、それは助けになりません。 設定にダイナミックタイプを使用することを検討しましたが、速度は少しの要因です。もし私がそうしたら、それが問題を解決するかどうかは分からない。

これは本当にありがとうございました。私の場合を助ける時間があることを願っています。

答えて

0

エラーメッセージが再現できませんでした。

コードにいくつかのタイプミスがありますが、それらをクリーンアップした後、残っている唯一のエラーはCleanupの定義にあり、特性を拡張するときにタイプパラメータとして渡すことはできません。あなたはそれ特色作り、そしてコンパニオンオブジェクトでそれを拡張することで、その周りを取得することができ、オブジェクトのクリーンアップ

を含む

object Cleanup extends Tool[Cleanup.type, CleanupSettings] { ... } 

違法循環参照。完全なコードは次のとおりです。

trait Tool[T <: Tool[T, U], U <: Settings[T]] { 
    // members here 
    def createSettingsFrom(settingsWithStringNames: Map[String, _]): U 
} 

trait Settings[T <: Tool[T, _ <: Settings[T]]] 

trait Cleanup extends Tool[Cleanup, CleanupSettings] { 
    override def createSettingsFrom(settings: Map[String, _]): CleanupSettings = CleanupSettings(
    settings.get("attribute1").asInstanceOf[Int], 
    settings.get("attribute2").asInstanceOf[String]) 
} 
object Cleanup extends Cleanup 

case class CleanupSettings(
    attribute1: Int, 
    attribute2: String) extends Settings[Cleanup] 
+0

これは非常にクールです。私はそれを簡素化するためにサブクラス型パラメータを削除してしまった。クリーンアップを参照することは、そのクラスを参照することと同じではありませんが、それは循環参照のための素晴らしい回避策です。ありがとうございました :) – Dico

関連する問題