cbsteh
cbsteh

Reputation: 889

Why (; [(:x, 1), (:y, 2)]...) creates a NamedTuple?

I'm still learning Julia, and I recently came across the following code excerpt that flummoxed me:

res = (; [(:x, 10), (:y, 20)]...)  # why the semicolon in front?
println(res)           # (x = 10, y = 20)
println(typeof(res))   # NamedTuple{(:x, :y), Tuple{Int64, Int64}}

I understand the "splat" operator ..., but what happens when the semicolon appear first in a tuple? In other words, how does putting a semicolon in (; [(:x, 10), (:y, 20)]...) create a NamedTuple? Is this some undocumented feature/trick?

Thanks for any pointers.

Upvotes: 2

Views: 383

Answers (1)

cbk
cbk

Reputation: 4370

Yes, this is actually a documented feature, but perhaps not a very well known one. As the documentation for NamedTuple notes:

help?> NamedTuple
search: NamedTuple @NamedTuple

  NamedTuple

  NamedTuples are, as their name suggests, named Tuples. That is, they're a tuple-like
  collection of values, where each entry has a unique name, represented as a Symbol.
  Like Tuples, NamedTuples are immutable; neither the names nor the values can be
  modified in place after construction.

  Accessing the value associated with a name in a named tuple can be done using field
  access syntax, e.g. x.a, or using getindex, e.g. x[:a]. A tuple of the names can be
  obtained using keys, and a tuple of the values can be obtained using values.

[... some other non-relevant parts of the documentation omitted ...]

  In a similar fashion as to how one can define keyword arguments programmatically, a
  named tuple can be created by giving a pair name::Symbol => value or splatting an
  iterator yielding such pairs after a semicolon inside a tuple literal:

  julia> (; :a => 1)
  (a = 1,)

  julia> keys = (:a, :b, :c); values = (1, 2, 3);

  julia> (; zip(keys, values)...)
  (a = 1, b = 2, c = 3)

  As in keyword arguments, identifiers and dot expressions imply names:

  julia> x = 0
  0

  julia> t = (; x)
  (x = 0,)

  julia> (; t.x)
  (x = 0,)

Upvotes: 3

Related Questions