2
我正在寻找一种方法来捕获在理解本身的类型中用于理解的类型。为此,我所指定的粗糙界面:用于理解的捕获和链接类型
trait Chain[A]{
type ChainMethod = A => A //type of the method chained so far
def flatMap[B](f: A => Chain[B]): Chain[B] //the ChainMethod needs to be included in the return type somehow
def map[B](f: A => B): Chain[B]: Chain[B]
def fill: ChainMethod //Function has to be uncurried here
}
作为例子的几个具体类型的Chain
:
object StringChain extends Chain[String]
object IntChain extends Chain[Int]
和壳体类,将被使用:
case class User(name:String, age:Int)
A链可以创建一个for理解:
val form = for{
name <- StringChain
age <- IntChain
} yield User(name, age)
的form
类型应该是
Chain[User]{type ChainMethod = String => Int => User}
,使我们可以做到以下几点:
form.fill("John", 25) //should return User("John", 25)
我尝试了一些方法,以结构类型和专业FlatMappedChain
特质,但我不能让类型系统按照我希望的方式行事。我会喜欢关于如何指定接口的一些想法或建议,以便编译器可以认识到这一点,如果这是可能的话。