Named tuples are similar to tuples in Julia, except in named tuples we can provide a name for each component in the named tuple.
In named tuples, we can access each component additionally with its name using dot syntax (tuple.name
).
(name=value, name=value)
Let's take a look at an example of this.
#declare and initialize tuplet = (a=5, b=10)#access element using indexprintln(t[1])#access element using nameprintln(t.a)
In the above code snippet:
Line 2: We declare and initialize the named tuple, t
.
Line 5: We use the index
to access the element.
Line 8: We use the element's name with dot syntax to access the same element of the named tuple, t
.