Stream: beginners

Topic: Roc's tags


view this post on Zulip Alex (May 03 2024 at 09:46):

Hi everyone!
I have a question.

I'm wondering if it's possible to use Roc's tags in low-level languages like C++, Rust, Zig, etc.
If no - then why, and if yes - why no one does it.
On surface, this feature looks like just anonimous Rust enums, where enum is not defined upfront by user, but is instead defined automatically by the compiler.

I'm also curious about closed/open feature which is related to tags as well as records.

Sorry if I'm asking in the wrong place.
Thanks!

view this post on Zulip Luke Boswell (May 03 2024 at 09:52):

Heres a rather long conversation related to tags

view this post on Zulip Luke Boswell (May 03 2024 at 09:53):

From memory that was an interesting read going into the weeds of what they are.

view this post on Zulip Luke Boswell (May 03 2024 at 09:53):

To answer your question more specifically, I'm not sure if it's possible. It's certainly possible to simulate them I guess :shrug:

view this post on Zulip Alex (May 03 2024 at 10:38):

Thanks, I'll take a look.

view this post on Zulip Richard Feldman (May 03 2024 at 10:40):

any language could do it for sure

view this post on Zulip Richard Feldman (May 03 2024 at 10:41):

OCaml has had a very similar language feature (which Roc's tags were originally based on) for a long time, called polymorphic variants

view this post on Zulip Richard Feldman (May 03 2024 at 10:42):

I think one reason nobody does it is that Roc is the first language to try making them the primary sum type the language uses

view this post on Zulip Richard Feldman (May 03 2024 at 10:43):

OCaml also has traditional nominal sum types (like Rust enums) that must be declared up front, and the general recommendation is to use those instead of polymorphic variants - there's a section at the end of that linked article about their reasoning

view this post on Zulip Richard Feldman (May 03 2024 at 10:44):

my intuition was that the concerns they outlined wouldn't be problems in practice in Roc, and fortunately that has definitely worked out as I'd hoped :big_smile:

view this post on Zulip Richard Feldman (May 03 2024 at 10:44):

but it might not have!

view this post on Zulip Richard Feldman (May 03 2024 at 10:45):

another relevant factor is that before Ayaz came up with a nicer syntax for open/closed, the type annotation syntax was confusing to a lot of beginners (now it's fine though)

view this post on Zulip Richard Feldman (May 03 2024 at 10:46):

so for all those reasons, the information that you can use them as a core language feature and have it be a nice experience is very new - we're the first ones to try that experiment - so it's not like other languages knew about that and chose to go a different way; rather, they didn't have the results of the experiment so it presumably wasn't even something on their radar!

view this post on Zulip Brendan Hansknecht (May 03 2024 at 15:06):

I think it is also important to add that anything that is a roc tag could be represented as a rust Enum. It just might be less convenient to do so cause in rust. you will have to explicitly name a bunch of this that roc would build automatically.

view this post on Zulip Nick Smith (Jul 27 2024 at 13:50):

Richard Feldman said:

I think one reason nobody does it is that Roc is the first language to try making them the primary sum type the language uses

I don’t mean to rain on anybody’s parade, but the OCaml veteran Andreas Rossberg created a blockchain language called Motoko a few years ago, where the only kind of sum type is polymorphic variants. The Motoko docs aren’t so great, but here’s someone blogging about the feature: https://kaipeacock.com/blog/motoko/using-variants/

The feature is more or less identical to Roc, I believe.
I don’t think Motoko is necessarily a good language, but I just thought I should point out some prior art :face_with_peeking_eye:.

view this post on Zulip Richard Feldman (Jul 27 2024 at 14:11):

that's cool! It looks like Folkert had already implemented tag unions in Roc's compiler prior to Motoko's initial commit, but I doubt Roc was on their radar. Most likely, we both independently decided polymorphic variants were worth trying out as the main sum type :big_smile:


Last updated: Jul 06 2025 at 12:14 UTC