Scala - 播放:如何将表单字段绑定到 Vector?

Scala - Play: How to bind a form field to a Vector?

在 Play 2.6 中,重复元素的默认表单绑定是 SeqListSet,例如:

Form(
  "name" -> seq(text)
)

但是我在几个场景中使用 Vector 集合类型,因为它比 SeqIterable 的默认实现具有更好的整体性能(例如基于索引的访问)List(如果进行索引搜索,处理 Head 或 Tail 元素线性时间的良好性能)。

在当前的 Play 2.6 中,VectorIndexedSeq(默认为 Vector)没有任何默认的表单绑定。

映射 Vector 的默认方法是什么?

您可以像这样定义自己的映射,

def vector[A](mapping: Mapping[A]): Mapping[Vector[A]] =
  RepeatedMapping(mapping).transform(_.toVector, _.toList)

然后像这样使用它,

Form(
  "name" -> vector(text)
)