struct NamedTuple(**T)
Overview
A named tuple is a fixed-size, immutable, stack-allocated mapping of a fixed set of keys to values.
You can think of a NamedTuple
as an immutable Hash
whose keys (which
are of type Symbol
), and the types for each key, are known at compile time.
A named tuple can be created with a named tuple literal:
language = {name: "Crystal", year: 2011} # NamedTuple(name: String, year: Int32)
language[:name] # => "Crystal"
language[:year] # => 2011
language[:other] # compile time error
See NamedTuple
literals in the language reference.
The compiler knows what types are in each key, so when indexing a named tuple with a symbol or string literal the compiler will return the value for that key and with the expected type, like in the above snippet. Indexing with a symbol or string literal for which there's no key will give a compile-time error.
Indexing with a symbol or string that is only known at runtime will return
a value whose type is the union of all the types in the named tuple,
and might raise KeyError
.
Indexing with #[]?
does not make the return value nilable if the key is
known to exist:
language = {name: "Crystal", year: 2011}
language[:name]? # => "Crystal"
typeof(language[:name]?) # => String
NamedTuple
's own instance classes may also be indexed in a similar manner,
returning their value types instead:
tuple = NamedTuple(name: String, year: Int32)
tuple[:name] # => String
tuple["year"] # => Int32
tuple[:other]? # => nil
Included Modules
Defined in:
collection/core_ext.crInstance Method Summary
-
#each_pair(&) : Nil
Must yield this structure's key, value or index, element pairs.
-
#each_pair : Iterator
Provides an
Iterator
for each pair inself
. -
#traverse(*prefix : *T, &) : Nil forall T
Traverse the structure depth first yielding a tuple of the path through the structure and the leaf value for every element to the passed block.
-
#traverse(*prefix : *T) : Iterator forall T
Provide an iterator that may be used to traverse a nested structure.
Instance methods inherited from module Collection(Symbol, NoReturn)
[](key_or_index : X) : Y
[],
[]?(key_or_index : X) : Y | Nil
[]?,
dig(key : X, *subkeys)
dig,
dig?(key : X, *subkeys)
dig?,
each_pair(&block : Tuple(X, Y) -> ) : Nileach_pair : Iterator(Tuple(X, Y)) each_pair, nested? : Bool nested?
Instance methods inherited from class Object
presence
presence
Instance Method Detail
Must yield this structure's key, value or index, element pairs.
Provides an Iterator
for each pair in self
.
NOTE when the base collection provides it own iterator implementation, this method should be overridden to use this directly, this is a generalised implementation and likely sub-optimal in many cases.
Traverse the structure depth first yielding a tuple of the path through the structure and the leaf value for every element to the passed block.
The paths contain a tuple of the keys or indicies used across
intermediate structures, similar to what would be passed to #dig
.
Provide an iterator that may be used to traverse a nested structure.