News for hackers. Stuff that matters.
Re:API design note: Beware of adding an "Other" enum value by rendaw on Sunday March 02, 2025 @10:00PM (#43237844)
I absolutely _hate_ this. Since you're forced to add a default case, if a new field is added in the future that you need to actively handle it won't turn into a compile error _or_ surface as a runtime error.

I think half of it is developers presuming to know users' needs and making decisions for them (users can make that decision by themselves, using the default case!) but also a logic-defying fear of build breakage, to the point that I've seen developers turn other compile errors into runtime errors in order to avoid "breaking changes".

Re:API design note: Beware of adding an "Other" enum value by bobbylarrybobby on Sunday March 02, 2025 @11:29PM (#43238308)
I agree, this is the one place where upstream crates should be allowed to make breaking changes for downstream users. As a consumer of another crate’s enum, it's easy to enough opt into “never break my code” by just adding default cases, but I'd like to have to opt into that so that I'm notified when new variants are added upstream. Maybe this should even be a Cargo.toml setting — when an upstream crate is marked non-exhaustive, the downstream consumer gets to choose: require me to add default cases (and don't mark them as dead code), or let me exhaustively match anyway, knowing my match statement might break in the future.
Re:API design note: Beware of adding an "Other" enum value by michaeljsmith on Sunday March 02, 2025 @10:04PM (#43237867)
Not sure about Rust, but Typescript allows you to have the default handling but still flag a compile error if a new field is added (the first is useful e.g. if a separate component is updated and starts sending new values).

https://stackoverflow.com/a/39419171/974188

Re:API design note: Beware of adding an "Other" enum value by Spivak on Sunday March 02, 2025 @10:47PM (#43238115)
https://news.ycombinator.com/item?id=43237013

You have to opt into it but it's nice that it's available.

Re:API design note: Beware of adding an "Other" enum value by rendaw on Sunday March 02, 2025 @11:47PM (#43238398)
Oh nice! That seems so backwards, but hey, if it works...
Re:API design note: Beware of adding an "Other" enum value by BlackFly on Monday March 03, 2025 @08:28AM (#43241509)
It is all about what constitutes a major version bump and what constitutes the public api.

If I have a parameter in my public API that has enumerated options, I should be able to add a new option without needing to bump my semver major version number since downstream existing code obviously isn't going to use it yet. If downstream was using my public api's enum for some of their own book keeping and so matched on my enum, I want to reserve the right to to say that that is non-public use of my enum, hence the idea that exhaustiveness in enums is a separate decision on to what is included in a public API or not.

On the other hand, if I introduce a new variant in a return value and existing code will get it and need to actually do something with it, then it should probably be breaking. Errors are somewhat of an exception to this since almost all error enumerations need a general, "unknown error" category anyways and introducing a new variant is generally elevating one case out of that general case. Obviously authors can make mistakes.

The alternative, when you cannot mark non_exhaustive, is to introduce stringly typed catch alls, which is much less desirable for everyone.

Re:API design note: Beware of adding an "Other" enum value by bmoxb on Monday March 03, 2025 @01:45PM (#43245231)
It arguably makes sense in a large monorepo, but otherwise I would agree.