A Binding holds the Scala functions that a zio.blocks.schema.Reflect node needs to work with values at run time: how to build a value, how to take it apart, and how to tell the cases of a sum type apart. A Reflect describes the shape of a type as plain data; its bindings make that description usable.
A Reflect node keeps its binding in its metadata F[T, A]. A Reflect.Bound node, whose F is Binding, can construct and deconstruct values, and every zio.blocks.schema.Schema wraps one. A Reflect.Unbound node carries a NoBinding instead: it holds no functions, so it can be serialized, and DynamicSchema#rebind gets its bindings back from a BindingResolver.
There is one binding kind per node kind (a Reflect.Deferred node uses the binding of the node it defers to), and the phantom type T names it (see BindingType):
- Binding.Record: a Constructor and a Deconstructor that move a record's fields in and out of Registers.
- Binding.Variant: a Discriminator that returns the case index of a value, and Matchers that downcast a value to each case.
- Binding.Seq: a SeqConstructor and a SeqDeconstructor for a collection type constructor.
- Binding.Map: a MapConstructor and a MapDeconstructor for a map type constructor.
- Binding.Primitive: a marker without functions.
- Binding.Wrapper:
wrapandunwrapfunctions between a type and the type it wraps. - Binding.Dynamic: a marker without functions, for a node that holds a zio.blocks.schema.DynamicValue.
Schema.derived creates the bindings of a whole schema, and Binding.of derives the binding of one type. The example takes the record binding of a derived schema and moves a value through registers:
import zio.blocks.schema.binding._
case class Book(title: String, year: Int)
object Book {
implicit val schema: Schema[Book] = Schema.derived
}
val record = Book.schema.reflect.asRecord.get
val registers = Registers(record.usedRegisters)
record.deconstructor.deconstruct(registers, RegisterOffset.Zero, Book("Zionomicon", 2021))
val title = registers.getObject(RegisterOffset.Zero) // "Zionomicon"
val book = record.constructor.construct(registers, RegisterOffset.Zero) // Book("Zionomicon", 2021)
Type parameters
- A
-
the type whose values the binding works with
- T
-
the binding kind, one of the BindingType members
Attributes
- See also
- Companion
- object
- Source
- Binding.scala
- Graph
-
- Supertypes
- Known subtypes