我正在寻找一种方法来捕获在推导本身的类型中for推导期间使用的类型。为此,我指定了一个粗略的接口:
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类:
case class User(name:String, age:Int)
可以使用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
特征,但我不能让类型系统按照我想要的方式行事。我希望能有一些关于如何指定接口的想法或建议,以便编译器可以识别这个,如果这是可能的。
我认为在Scala中从头开始做这些是相当困难的。您可能需要为不同的函数定义许多隐式类。
如果您使用为类型级计算设计的shapeless
库,则会变得更容易。下面的代码使用了一种稍微不同的方法,其中Chain.fill
是一个从参数元组到结果的函数。flatMap
的实现还允许将多个表单合并为一个:
import shapeless._
import shapeless.ops.{tuple => tp}
object Chain {
def of[T]: Chain[Tuple1[T], T] = new Chain[Tuple1[T], T] {
def fill(a: Tuple1[T]) = a._1
}
}
/** @tparam A Tuple of arguments for `fill`
* @tparam O Result of `fill`
*/
abstract class Chain[A, O] { self =>
def fill(a: A): O
def flatMap[A2, O2, Len <: Nat, R](next: O => Chain[A2, O2])(
implicit
// Append tuple A2 to tuple A to get a single tuple R
prepend: tp.Prepend.Aux[A, A2, R],
// Compute length Len of tuple A
length: tp.Length.Aux[A, Len],
// Take the first Len elements of tuple R,
// and assert that they are equivalent to A
take: tp.Take.Aux[R, Len, A],
// Drop the first Len elements of tuple R,
// and assert that the rest are equivalent to A2
drop: tp.Drop.Aux[R, Len, A2]
): Chain[R, O2] = new Chain[R, O2] {
def fill(r: R): O2 = next(self.fill(take(r))).fill(drop(r))
}
def map[O2](f: O => O2): Chain[A, O2] = new Chain[A, O2] {
def fill(a: A): O2 = f(self.fill(a))
}
}
你可以这样使用它:
scala> case class Address(country: String, city: String)
defined class Address
scala> case class User(id: Int, name: String, address: Address)
defined class User
scala> val addressForm = for {
country <- Chain.of[String]
city <- Chain.of[String]
} yield Address(country, city)
addressForm: com.Main.Chain[this.Out,Address] = com.Main$Chain$$anon$2@3253e213
scala> val userForm = for {
id <- Chain.of[Int]
name <- Chain.of[String]
address <- addressForm
} yield User(id, name, address)
userForm: com.Main.Chain[this.Out,User] = com.Main$Chain$$anon$2@7ad40950
scala> userForm.fill(1, "John", "USA", "New York")
res0: User = User(1,John,Address(USA,New York))