https://speakerdeck.com/folone/theres-a-prolog-in-your-scalaを考慮して、Scala 型システムを「悪用」してCanBuildFrom
、指定された基準に一致する eg のすべてのインスタンスを見つけたいと考えています。プロローグ スタイルでは、次の疑似コードの行で何かを評価します。
can_build_from(Src, int, list[int])
Src = somecollectiontype1[int]
Src = somecollectiontype2[int]
... etc
つまり、ランタイムはSrc
ステートメントを満たすすべての値を検索しますcan_build_from(Src, int, list[int])
。
さて、Scala の暗黙的なルックアップ システムであるプリミティブな制約/ロジック プログラミング環境は、そのようなトリックに使用することを意図しておらず、複数の見つかった値を "返す" ことができないことを認識していますSrc
。ボックスなので、私の質問は次のとおりです。何らかの方法で in のすべての可能な値を取得できるようにするための「魔法のトリック」はありX
ますCanBuildFrom[X, Int, List[Int]]
か?
追加の例:
trait CanFoo[T, U]
implicit val canFooIntString = new CanFoo[Int, String] {}
implicit val canFooDblString = new CanFoo[Double, String] {}
implicit val canFooBoolString = new CanFoo[Boolean, String] {}
implicit val canFooIntSym = new CanFoo[Int, Symbol] {}
implicit val canFooDblSym = new CanFoo[Double, Symbol] {}
implicit val canFooBoolSym = new CanFoo[Boolean, Symbol] {}
CanFoo[X, String]
ここで、クエリを実行して を取得しX ∈ [Int, Double, Boolean]
たりCanFoo[Int, X]
、 を取得したりしたいと考えていますX ∈ [String, Symbol]
。
または、CanFoo[X, String]
を返します。つまり、その一致List(canFooIntString, canFooDblString, canFooBoolString)
のすべてのインスタンスです。CanFoo