What are named tuples in Julia?

Overview

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).

Syntax

(name=value, name=value)

Let's take a look at an example of this.

Example

#declare and initialize tuple
t = (a=5, b=10)
#access element using index
println(t[1])
#access element using name
println(t.a)

Explanation

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.

Free Resources