logoalt Hacker News

12_throw_away • yesterday at 11:22 PM • 1 reply • view on HN

Because sometimes you just want to talk about the types of variants without having to construct a value. But, you probably don't want to manually maintain a separate enum just for the discriminants. Nor do you want to manually maintain the mappings between the original enum and the discriminant-only enum.

I've had it come up when dealing with ASTs, although I don't recall the specifics. This is a problem that's certainly pretty easy to solve with macros. I'm pretty sure last time I ran into this I used `strum` [1] and its `EnumDiscriminants` macro to handle this.

[1] https://lib.rs/crates/strum


Replies

slopinthebag • yesterday at 11:30 PM

ahh ok ive run into this before, yes either strum or i assumed i was doing something non-optimal if i had to talk about types like this