Binding

zio.blocks.schema.binding.Binding
See theBinding companion object
sealed trait Binding[T, +A]

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):

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
class Object
trait Matchable
class Any
Known subtypes
class Dynamic
class Map[M, K, V]
class Primitive[A]
class Record[A]
class Seq[C, A]
class Variant[A]
class Wrapper[A, B]
Show all
In this article