Tuple documentation is confusing for me

The tuple official documentation here is confusing.

To append to a tuple, it is preferable to use pattern matching:

tuple = {:ok, :example}

# Avoid
Tuple.insert_at(tuple, 2, %{})

# Prefer
{:ok, atom} = tuple
{:ok, atom, %{}}

Can someone explain to me the statements below # Prefer? I do not understand how the map got appended.

It’s just simply a new tuple. You could rebind it to the tuple variable if that helps make it clearer:

tuple = {:ok, :example}
{first, second} = tuple
tuple = {first, second, %{}}
1 Like

If that’s what this is saying, the wording really is confusing.

To append to a tuple, it is preferable to use pattern matching:

Shouldn’t this say something like

It is preferable to create a new tuple instead

1 Like

The following is clearer to my eyes (I added result =):

tuple = {:ok, :example}

# Avoid
result = Tuple.insert_at(tuple, 2, %{})

# Prefer
{:ok, atom} = tuple
result = {:ok, atom, %{}}

I think it helps call out how the pattern matching is simply an intermediate step, i.e., binding the :example value to atom, purely for the benefit of constructing the longer tuple.

2 Likes

@benwilson512, @Ted I understand now. I thought that

{:ok, atom, %{}}

Was the result of the previous statement.

@Ninigi I totally agree with your wording.

Thank you very much!

Submitted a doc update as a pull request:

Edit: And it’s merged! :confetti_ball:

7 Likes

@axelson Great! Thank you!

2 Likes

I had to stare at it for a while, too.

@axelson Thanks!

1 Like