/docs/40/40.md

http://github.com/osinka/subset · Markdown · 43 lines · 30 code · 13 blank · 0 comment · 0 complexity · 8a5943b384e0f85faf210b36bb938ab3 MD5 · raw file

  1. # Tuples
  2. Sometimes you may want to serialize fields in batches, e.g. two or
  3. three at a time. __Subset__ has a mechanism for this, it is a `Tuple`
  4. serializer.
  5. Assuming there is a couple of fields defined
  6. ```scala
  7. val f = "f".fieldOf[Int]
  8. val g = "g".fieldOf[String]
  9. ```
  10. An expression `f ~ g` will create a serializer (and extractor as well)
  11. for `Tuple[Int,String]`. Thus:
  12. ```scala
  13. val Tfg = f ~ g
  14. val mutation = Tfg( 10 -> "str" )
  15. ```
  16. Here `mutation` will be equivalent to `f(10) ~ g("str")`.
  17. And the extractor will look like:
  18. ```scala
  19. dbo match {
  20. case Tfg(i, s) =>
  21. case smthElse =>
  22. }
  23. ```
  24. Tuples of higher arity are possible too:
  25. ```scala
  26. val T3 = Tfg ~ "b".fieldOf[Boolean]
  27. ```
  28. > Tuple serializers/deserializers depend on all of their types
  29. > `ValueWriter`s and `ValueReader`s respectively.
  30. * * *