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
The compiler knows what types are in each key, so when indexing a named tuple with a symbol literal the compiler will return the value for that key and with the expected type, like in the above snippet. Indexing with a symbol literal for which there's no key will give a compile-time error.
Indexing with a symbol 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
.
Defined in:
to_con.crfrom_con.cr
Class Method Summary
Instance Method Summary
Instance methods inherited from struct Value
==(other : CON::Any)
==
Instance methods inherited from class Object
===(other : CON::Any)
===,
from_con(con : String | IO)
from_con,
to_con(io : IO)to_con to_con, to_pretty_con(indent : String = " ")
to_pretty_con(io : IO, indent : String = " ") to_pretty_con