Learn R Programming

sicher (version 0.1.0)

ListOf: Create a homogeneous list type

Description

Produces a type that validates a list whose every element satisfies the provided element type. This is useful when you expect a list of similar records (e.g. parsed JSON array). You can further constrain the length with the size operator: `ListOf(User)[10]`.

Usage

ListOf(element_type)

Value

A sicher_type that checks the value is a list and that all elements conform to `element_type`.

Arguments

element_type

A sicher_type or sicher_union describing each element.

Examples

Run this code
# Define an inner record type
Record <- create_list_type(list(id = Numeric, name = String))

# Now require a list of records
Records <- ListOf(Record)
records %:% Records %<-% list(
  list(id = 1, name = "a"),
  list(id = 2, name = "b")
)

# fixed-size list of ten records
TenRecs <- Records[10]
# will throw if length != 10

Run the code above in your browser using DataLab