News for hackers. Stuff that matters.
Re:Enums in Go by weavie on Friday July 12, 2024 @04:59AM (#40943861)
The problem isn't so much the lack of enums, it's more that there is no way to do exhaustive pattern matching at compile time. I have seen production systems go down because someone added a variant to an 'enum' but failed to handle that new variant everywhere.
Re:Enums in Go by K0nserv on Friday July 12, 2024 @05:27AM (#40944004)
The overarching lesson of my career has been that people are fallible and processes that rely on humans to not make mistakes are bound to fail. Recently I've also been thinking about the importance of being able to reason "locally" about code, it might be the single most important property of a software system. "locally" typically means a function, class, or module, but I think it can also be applied to "horizontal" cases like this. For example, if you add an enum variant the compiler should guide you to reason about each place where the enum is no longer exhaustively matched.
Re:Enums in Go by aleksi on Friday July 12, 2024 @05:21AM (#40943979)
There are two linters that add those checks: https://github.com/nishanths/exhaustive for values and https://github.com/alecthomas/go-check-sumtype for types. Both are integrated into golangci-lint. I use both a lot and have only a positive experience.

Having support from the language would be nice, though.

Re:Enums in Go by tialaramex on Friday July 12, 2024 @06:30AM (#40944266)
Yeah, it makes a huge difference whether this is the default, and that's not a Go thing, it's not a programming language thing, it's the whole of human existence.

Literacy is an example. We're used to a world where it's just normal that other humans can make marks and interpret our own marks as to meaning. A thousand years ago that would be uncommon, ten thousand years ago vanishingly rare.

I really celebrate tools which take an idea that everybody agrees is a good idea, and bake it in so that everybody just has that, rather than agreeing it's a good idea but, eh, I have other things to do right now.

And it annoys me when I see these good ideas but they're left as an option, quietly for a few people to say "Oh, that's good to see" and everybody else misses out, as if "Literacy" was an optional high school class most of your peers didn't take and now they can't fucking read or write.

Example: Git has a force push feature, necessarily we can (if we have rights) overwrite a completely unrelated branch state, given any state X, now the state is our state Y instead. This isn't the default, that part is fine... Git also has "force-with-lease". This is a much better feature. Force-with-lease says "I know the current state of this branch is X, but I want to overwrite it anyway". If we're wrong, and X is not (any longer perhaps) the current state, the push fails. But force-with-lease isn't the default.

[Edited to fix clumsy wording in the last sentence]

Re:Enums in Go by dizzyVik on Friday July 12, 2024 @08:52AM (#40945080)
These both look like great tools. I'll give them a spin!
Re:Enums in Go by ReleaseCandidat on Friday July 12, 2024 @06:06AM (#40944168)
In theory yes. But practically there are always locations where we can't match every case, so we either have to live with a warning or add a catch-all arm. And as soon as a catch-all arm exists, we are in "not checked any more" state, but with a compiler that is supposed to check for exhaustiveness. Which is way worse if the catch-all arm isn't a `panic("Unhandled match branch!")`.

Yes, you can counter that with GADTs and match against exhaustive subsets. But to ergonomically handle these cases, you need something like Pattern Synonyms or you drown in boilerplate.

Re:Enums in Go by K0nserv on Friday July 12, 2024 @06:15AM (#40944202)
You don't have to add a catch-all arm, you can still explicitly match i.e

    match value {
      A => { do_stuff() },
      B => { unreachable!("B is not applicable here because <reason>") },
    }
now when you add C you still have to match it(given the enum is exhaustive).
Re:Enums in Go by ReleaseCandidat on Friday July 12, 2024 @06:20AM (#40944222)
Yes, I see, I should have mentioned that.

Same as above: this solution stops working as soon as you're handling 5 out of 50 cases (or, more realistically, 10 out of 200). Lexical tokens are which always trigger the mentioned problems in my code - often you match against subsets, as there are _way_ too many of them to add them all explicitly.

Re:Enums in Go by rty32 on Friday July 12, 2024 @06:57AM (#40944406)
First of all, I think there is a problem with your design if you have 50 or 200 cases. Most of the code I have seen -- in any language -- have no more than 10 cases, rarely 20. Maybe that is the issue to look at first.

Then, this "limitation" is not an argument for not running the exhaustive check. In the vast majority of cases where there are about 5 enum entries and most cases need their own path, they would be explicitly written out (i.e. no _ =>), and this works extremely well. I have had good experience, and I believe other people can attest this.

If you only have 1 case matched and everything else goes in _, later needs to add one case, you just do that, likely in every other language, there is nothing that can help. But what's described above is already a big improvement.

Re:Enums in Go by ReleaseCandidat on Friday July 12, 2024 @07:09AM (#40944468)
> this "limitation" is not an argument for not running the exhaustive check.

That's not what I wanted to express. What I wanted to say is, that even when using Haskell, which has all the possibilities to actually handle matches of subsets quite ergonomically, you can't be sure that there isn't at least one čase which isn't caught by the exhaustiveness checker (and sometimes it's just wrong, but then we're not talking about enums). So you always have to check all manually, but the checker makes that easier.

Re:Enums in Go by lawn on Friday July 12, 2024 @06:48AM (#40944360)
This is true, but in my experience these are edge cases and explicitly matching everything works well 90% of the time.
Re:Enums in Go by ReleaseCandidat on Friday July 12, 2024 @07:03AM (#40944436)
This is actually the problém. I'd even say that it works like 95% of the time, that's why people (of course, I don't make such silly mistakes ;) aren't used to check where it matters. In reality the policy must be to always (whether there are or aren't working exhaustiveness checks) manually check each and every occurence when adding. Don't get mé wrong, I prefer having exhaustiveness checks, but they make the manually searching a bit less tedious, not alleviate it in total.
Re:Enums in Go by gray_-_wolf on Friday July 12, 2024 @06:14AM (#40944193)
As long as the set is limited (as enums usually are), you can always go with the middle ground between omitted case and catch all:

    case FILE_NOT_FOUND:
        /* This is normal, nothing to do. */
        break;
This should still catch adding new value into the enum and not handling it.
Re:Enums in Go by ReleaseCandidat on Friday July 12, 2024 @06:19AM (#40944219)
I should have added that this solution stops working as soon as you're handling 5 out of 50 cases. Lexical tokens are which always trigger the mentioned problems in my code - often you match against subsets, are there are _way_ too many of them to add them all explicitly.
Re:Enums in Go by dgb23 on Friday July 12, 2024 @05:18AM (#40943963)
Isn’t that the first thing you would do if you add a new variant?
Re:Enums in Go by K0nserv on Friday July 12, 2024 @05:46AM (#40944082)
Only if you remember, which you, or someone else, is bound to not eventually
Re:Enums in Go by dgb23 on Friday July 12, 2024 @08:10AM (#40944793)
But you're not adding variants without a reason? You want them to have some effect.

It's hard for me to think of an example where it would make even sense to "having to remember to handle the variant" rather than "handling the desired effect of the variant".

Re:Enums in Go by K0nserv on Friday July 12, 2024 @08:25AM (#40944882)
People are stressed, get distracted, are tired, don't have complete knowledge etc. This is kind of like arguing that null pointers aren't a problem, you "just" have to check all usage of the pointer if you make it null. In practice we know solutions like this don't work
Re:Enums in Go by rty32 on Friday July 12, 2024 @06:37AM (#40944307)
added that every switch/if should handle this exhaustively. For any project with more than a few dozens of files, it is basically impossible to remember all the downstream code that uses the enum -- you have to track it down, or better, let compiler automate check all usages
Re:Enums in Go by weavie on Friday July 12, 2024 @08:56AM (#40945108)
Huge codebase. It was handled in 13 places, but they missed the 14th.