This is comparing NamedTuple and Array. They are different datatypes with different design goals, so don’t support all the same syntax options. A (named) tuple is a struct-like heterogeneous object while an array is a indexable object (that usually has homogeneous elements).
However, there are many packages that have array objects that offer the ability to name the axes and/or indices of arrays (each package offers different designs and tradeoffs). Hopefully someone else can chime in with actual recommendations, but so far I’ve found (in no particular order) AxisArrays.jl, DimensionalData.jl, LabelledArrays.jl, NamedArrays.jl that all appear to provide some version of functionality like this.
You’re not really naming elements in the matrix.
The ones with round parentheses are either a sequence of expressions, i.e.
a = (b = 1; c=2)
is the same as
a = begin
b = 1
c = 2
end
So you just assign 1 to b, 2 to c, and a gets the value of the compound expression, which is the value of the last expression.
Or, with a comma, a = (b=1, c=2), you create a named tuple.
The array syntax does not in general allow naming of elements, but apparently there is some quirk in the parser which inadvertently allows assignments in the matrix notation so that a = [b=1 c=2; d=3 e=4] works, and ends up effectively as
b = 1
c = 2
d = 3
e = 4
a = [1 2; 3 4]
I’d guess it’s because this is parsed to some iteration which ends up as a hvcat call, but I’m not certain what happens.
Note that you can’t change the matrix by assigning e.g. to e afterwards, and e won’t change if you modify a[2, 2]. So the matrix elements have not been named. It’s merely in-place assignments, similar to constructions like sin((x=pi/2;)).
That’s… quite horrifying. Maybe someone can find a rare clever use for
julia> for Base.exp(x) in 1:5; @show exp('c'); end
exp('c') = 1
exp('c') = 2
exp('c') = 3
exp('c') = 4
exp('c') = 5
but the same can be accomplished in other ways, and in practice this syntax seems guaranteed to occur via typos or beginner mistakes in loops. And it makes them weirder and harder to figure out.
To be honest, I don’t find NamedTuple really useful in practice.
If the size of the tuple is small, I can index by integers directly because I can remember the location.
If it becomes complicated, I can use Dict instead.
I like the existing behavior, I hope we can retain this property.
I guess no one will write these 2 lines seriously in his work. It seems pointless, therefore less disquieting.
NamedTuples are fine when one needs to return multiple values from a function and doesn’t want to create a struct only for this purpose. It’s an on the fly struct.
Dicts are slow to lookup, whereas lookup in a named tuple is typically compiled away. With heterogeneous types, using a Dict will typically lead to dynamic dispatch, which is devastating for performance.
NamedTuples are more restricted than Dicts (they are immutable and their keys must be Symbols), but in cases where this is natural, I think that most experienced Julia users will prefer them over Dicts and Tuples, consider this example:
disc = (; radius=5.5, thickness=0.8, material=:steel)
# In another part of the code
weight = disc.thickness * pi * disc.radius^2 * density[disc.material]
compared with
disc = (5.5, 0.8, :steel)
# In another part of the code
weight = disc[1] * pi * disc[2]^2 * density[disc[3]]
or
disc = Dict(:radius => 5.5, :thickness => 0.8, :material => :steel)
# In another part of the code
weight = disc[:thickness] * pi * disc[:radius]^2 * density[disc[:material]]
In my opinion, the NamedTuple version is the clearest; it is much easier to understand than the Tuple version and looks neater and requires less keystrokes than the Dict version. Also considering the performance described by @sgaure , there are many cases where it should be the chosen solution.