Literals: NamedTuple

NamedTuple

A NamedTuple is typically created with a named tuple literal:

tuple = {name: "Crystal", year: 2011} # NamedTuple(name: String, year: Int32)
tuple[:name] # => "Crystal" (String)
tuple[:year] # => 2011      (Int32)

To denote a named tuple type you can write:

# The type denoting a named tuple of x: Int32, y: String
NamedTuple(x: Int32, y: String)

In type restrictions, generic type arguments and other places where a type is expected, you can use a shorter syntax, as explained in the type grammar:

# An array of named tuples of x: Int32, y: String
Array({x: Int32, y: String})

A named tuple key can also be a string literal:

{"this is a key": 1}

To the extent possible under law, the persons who contributed to this workhave waived
all copyright and related or neighboring rights to this workby associating CC0 with it.
https://crystal-lang.org/docs/syntax_and_semantics/literals/named_tuple.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部