type

TypedDict

@Js
const class TypedDict : Obj, Dict

TypedDict wraps a dict that maps tags to statically typed fields. To use this API:

  1. Create subclass of TypedDict
  2. Annotate const instance fields with @TypedTag
  3. Create constructor with Dict and it-block callback
  4. Optionally create convenience factory that calls create

The following coercions are supported:

  • Int field from Number tag
  • Duration field from Number tag
  • Bool field from Marker tag

Example:

const class ExampleRec : TypedDict
{
  static new wrap(Dict d, |Str|? onErr := null) { create(ExampleRec#, d, onErr) }

  new make(Dict d, |This| f) : super(d) { f(this) }

  @TypedTag const Int limit := 99

  @TypedTag const Duration timeout := 3sec
}

constructors

make

Sub constructor.

methods

create

Factory to create for given type and dict to wrap.

each

Iterate the wrapped dict tags

eachWhile

Iterate the wrapped dict tags until callback returns non-null

get

Get a tag from wrapped dict

has

Return if wrapped dict has given tag

isEmpty

Return if wrapped dict is empty

meta

Wrapped dict for this instance

missing

Return if wrapped dict is missing given tag

trap

Trap on the wrapped dict

Slot Details

create

static TypedDict create(Type type, Dict meta, |Str|? onErr := null)

Factory to create for given type and dict to wrap. Invalid tag values are logged to the given callback if provided.

each

virtual override Void each(|Obj,Str| f)

Iterate the wrapped dict tags

eachWhile

virtual override Obj? eachWhile(|Obj,Str->Obj?| f)

Iterate the wrapped dict tags until callback returns non-null

get

@Operator
virtual override Obj? get(Str n, Obj? def := null)

Get a tag from wrapped dict

has

virtual override Bool has(Str n)

Return if wrapped dict has given tag

isEmpty

virtual override Bool isEmpty()

Return if wrapped dict is empty

make

new make(Dict meta)

Sub constructor.

meta

Dict meta()

Wrapped dict for this instance

missing

virtual override Bool missing(Str n)

Return if wrapped dict is missing given tag

trap

virtual override Obj? trap(Str n, Obj?[]? a := null)

Trap on the wrapped dict