Tuple, Array length confusion


#1

A hopefully simple question. In 0.5.0 I find the following behaviour perfectly intuitive:

julia> length( [0.4,0.6,0.8] )
3

julia> length( (0.4,0.6,0.8) )
3

But the following feels deeply counterintuitive for tuples:

julia> length( [[0.4,0.6,0.8]] )
1

julia> length( ([0.4,0.6,0.8]) )
3

Could someone help as to why the tuple is ignored in the last case?


#2

([0.4,0.6,0.8]) doesn’t create a tuple, just like (1+2) does not create a tuple, it is just a means to explicitly state that it is important to you that whatever is in it is evaluated before interacting with the surrounding, for example (1+2)*2 .

If you want to create a tuple, use a ,

julia> length( ([0.4,0.6,0.8],) )
1

#3

It’s not a tuple at all. When creating single-element tuples, you need to use a trailing comma. Otherwise the parentheses are just used for precedence groupings. E.g., (1) is just 1, but (1,) is a one-tuple.

length( ([.4,.6,.8],) )


#4

Thanks very much both. Got it :slight_smile: