tuple
tuple(xs...)
Construct a tuple of the given objects.
Examples
julia> a = (1,2,3)
(1,2,3)
julia> a[3]
3
julia> a = (1,2,3)
a[3]
a[1] = 2
`setindex!` has no method matching setindex!(::(Int64,Int64,Int64), ::Int64, ::Int64)
while loading In[5], in expression starting on line 3
-
Create a tuple with multiple elements:
julia> tuple(1, 2, 3, 4) (1, 2, 3, 4)
This example creates a tuple with four elements: 1, 2, 3, and 4.
-
Create a tuple with a single element:
julia> tuple("Hello") ("Hello",)
Even when creating a tuple with a single element, Julia requires a trailing comma to distinguish it from a regular value.
-
Create an empty tuple:
julia> tuple() ()
This example demonstrates creating an empty tuple with no elements.
- Create a tuple from an array:
julia> arr = [10, 20, 30] julia> tuple(arr...) (10, 20, 30)
By using the splat operator
...
, an array can be converted into a tuple.
Common mistake example:
julia> tuple([1, 2, 3])
([1, 2, 3],)
In this example, the tuple
function is called with an array as an argument. However, instead of converting the array elements into a tuple, it creates a tuple with the array itself as a single element. To convert an array into a tuple, use the splat operator ...
to expand the array elements within the tuple
function call.
See Also
User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.