Tuple, Array length confusion


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

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

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

But the following feels deeply counterintuitive for tuples:

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

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

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


([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],) )


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],) )


Thanks very much both. Got it :slight_smile: