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!
Heres a rather long conversation related to tags
From memory that was an interesting read going into the weeds of what they are.
To answer your question more specifically, I'm not sure if it's possible. It's certainly possible to simulate them I guess :shrug:
Thanks, I'll take a look.
any language could do it for sure
OCaml has had a very similar language feature (which Roc's tags were originally based on) for a long time, called polymorphic variants
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
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
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:
but it might not have!
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)
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!
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.
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:.
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