Stream: ideas

Topic: 1-tuples


view this post on Zulip Andy Ferris (Nov 20 2024 at 03:55):

Richard Feldman said:

that obviously looks like a mistake, plus it suggests that (foo, bar,) should be valid as well for a 2-tuple

Are optional trailing commas not expected to be allowed in Roc? That's probably my least-favourite thing about JSON.

If you do allow trailing commas in dynamically sized collections like arrays, it would seem to be an odd special case to forbid them in tuples, structs, or other comma-seperated syntax.

And if you allow 2-tuples, 3-tuples, 4-tuples, etc I would argue it a very odd special case to disallow 1-tuples (or even 0-tuples, being similar to or the same as an empty record).

view this post on Zulip Richard Feldman (Nov 20 2024 at 04:05):

oh actually we do allow trailing commas, although I don't think I've ever seen them used in single-line tuples like that :sweat_smile:

view this post on Zulip Richard Feldman (Nov 20 2024 at 04:05):

Andy Ferris said:

If you do allow trailing commas in dynamically sized collections like arrays, it would seem to be an odd special case to forbid them in tuples, structs, or other comma-seperated syntax.

And if you allow 2-tuples, 3-tuples, 4-tuples, etc I would argue it a very odd special case to disallow 1-tuples (or even 0-tuples, being similar to or the same as an empty record).

this is an interesting point...I'd never thought of it that way!

view this post on Zulip Brendan Hansknecht (Nov 20 2024 at 04:06):

In roc, the rest of the syntax makes a 1 tuple or 0 tuple unnatural. They are just parens for precedence.

view this post on Zulip Brendan Hansknecht (Nov 20 2024 at 04:06):

So they don't just fall out of the syntax

view this post on Zulip Richard Feldman (Nov 20 2024 at 04:06):

yeah certainly calling (foo) a 1-tuple would be weird

view this post on Zulip Richard Feldman (Nov 20 2024 at 04:07):

but it's a fair point that:

view this post on Zulip Richard Feldman (Nov 20 2024 at 04:08):

I'm not saying we should embrace it, but that is a coherent argument for 1-tuples "falling out of" trailing commas in collection literals

view this post on Zulip Notification Bot (Nov 20 2024 at 04:11):

8 messages were moved here from #ideas > custom types by Richard Feldman.

view this post on Zulip Richard Feldman (Nov 20 2024 at 04:12):

currently the formatter removes trailing commas in single-line types

view this post on Zulip Richard Feldman (Nov 20 2024 at 04:12):

so if we did want to support 1-tuples, we'd have to have the formatter keep them in

view this post on Zulip Richard Feldman (Nov 20 2024 at 04:12):

and (foo,) definitely still looks like a mistake to me :stuck_out_tongue:

view this post on Zulip Richard Feldman (Nov 20 2024 at 04:13):

and then I guess you'd destructure it with like (x,) = onetuple

view this post on Zulip Richard Feldman (Nov 20 2024 at 04:13):

which also looks like a mistake

view this post on Zulip Richard Feldman (Nov 20 2024 at 04:14):

a consistency argument against the idea of 1-tuples is that in every other collection literal, trailing commas are optional; they can be omitted without changing the meaning of the literal

view this post on Zulip Richard Feldman (Nov 20 2024 at 04:14):

and in the case of a 1-tuple that would not be the case, since (foo) is not a tuple at all

view this post on Zulip Richard Feldman (Nov 20 2024 at 04:14):

so there's a consistency counterargument too

view this post on Zulip Brendan Hansknecht (Nov 20 2024 at 04:36):

I guess the only value of one tuples is working with a function that accepts any tuple and only calls .0 on it?

view this post on Zulip Brendan Hansknecht (Nov 20 2024 at 04:36):

Like a function that only interacts with the first arg of a tuple.

view this post on Zulip Brendan Hansknecht (Nov 20 2024 at 04:37):

I don't think there is any other tangible gain in roc from it

view this post on Zulip Richard Feldman (Nov 20 2024 at 05:10):

that and making wrapper custom types more concisely than using a single-field custom record

view this post on Zulip Richard Feldman (Nov 20 2024 at 05:10):

which doesn't seem like a big advantage to me :big_smile:

view this post on Zulip Oskar Hahn (Nov 20 2024 at 06:01):

One feature of the zig formatter is, that if you set a trailing comma, it puts every element on its own line. And if you remove the trailing comma, it removes the newlines and puts all elements in the same line.

I like this feature. Maybe this is something you could think about adding to the Roc formatter.

But this feature would be strange for single tuples, if they need a trailing comma. It would be strange if you could online define a single tuples with multiple lines.

view this post on Zulip Andy Ferris (Nov 21 2024 at 00:52):

I do agree that (foo,) looks strange. It still looks strange to me! Yet I have found its presence elsewhere useful.

To me it's just the natural fallout of using ( ... ) brackets for two distinct purposes: as parentheses/expression grouping and as tuples. Given that design decision, using (foo,) as a one-tuple and (foo) as parentheses around foo is just a consequence of that initial design, nothing more or less. It's the syntax tradeoff that eventually rears its head because you took advantage of round brackets for two separate things.

That zig formatter rule seems pretty cool, though arguably multiline tuples are a bit of a code smell, so one could use it for arrays but not tuples for example, or disable it for all single-element collections, etc.


Last updated: Jun 16 2026 at 16:19 UTC