Working with NamedTuples
The NamedTuple
abstract type is a convenient immutable data structure that allows its elements to be accessed by name and by index. A typical way of thinking about them is that they are tuples with optional name information attached to data slots. However, there is one important distinction between these two types. While tuples are covariant, named tuples are invariant. In other words, NamedTuple
, in terms of subtyping, behaves like any other struct
type. This has important consequences for the method dispatch mechanism.
Getting ready
Note
In the GitHub repository for this recipe you will find the commands.txt
file that contains the presented sequence of shell and Julia commands.
Now open your favorite terminal to execute commands.
How to do it...
The first step is to create two tuples and two named tuples:
julia> t1 = (1, 2) (1, 2) julia> t2 = (1.0, 2) (1.0, 2) julia> nt1 = (a=1, b=2) (a = 1, b = 2) julia> nt2 = (a=1.0, b=2) (a = 1.0, b = 2)
Now, because...