Create empty tuple


#1

What is the most idiomatic way to create an empty - but typed - tuple? I’ve come up with a number of ways of doing it but not sure what the “right way” is.

Thanks,

Tom


#2

Please ignore. I now realize this doesn’t really make sense in Julia.


#3

For reference (for people who find this in some search), tuples are immutable and type each field, so there’s no reason to set the types because it will set the types to match the current values (since it’s immutable, the value cannot change anyways). Also, you cannot set the types of a tuple to later fill it because, again, it’s an immutable structure so you cannot “make it empty” and change it later.

If what you’re trying to do involves first allocating and then changing the structure, you want to use a mutable structure like an Array or a StaticArray (a fast array of fixed size) from StaticArrays.jl.


#4

I’d argue that the empty tuple and its type are both things that exist and make sense. They just have highly specialized use cases.

In particular, the type of the empty tuple is always the same type: it’s exactly Tuple{} since there are no elements whose type could determine the type of the tuple.


#5

Hi Chris,

Thank you for the answer. Just to clarify, I was actually trying to create an instance of

NTuple{0, Int}

So while ultimately not correct, at least I hope this clarifies that there was at least some “sense” to my original question.

Thanks,

Tom