lloydmeta / frunk

Funktional generic type-level programming in Rust: HList, Coproduct, Generic, LabelledGeneric, Validated, Monoid and friends.
https://beachape.com/frunk/
MIT License
1.24k stars 56 forks source link

add `extract` for getting the value out of a 1-coproduct #201

Closed rosefromthedead closed 1 year ago

rosefromthedead commented 1 year ago

I would find it useful :) It's like .take().unwrap() but without the implication that it might panic.

lloydmeta commented 1 year ago

Interesting; can you please explain your use case for making a Coproduction of 1 type?

ExpHP commented 1 year ago

Idk this is the author of effing-mad so if they have a use case, I believe them 🤣

rosefromthedead commented 1 year ago

Sorry, I tried to reply this morning, but my email server fell over :)

My API involves generic coproducts, and users* provide functions from a subset of a coproduct to a subset of another - providing multiple functions until all variants are accounted for. If the user wants to deal with only one variant of each, it is more ergonomic for me to provide API surface that does the extracting and injecting around such a function, so the user doesn't have to. These APIs are internally implemented using the more general public API, so my code has a fair few match expressions that do what extract could do.

*users (hypothetical). No one actually uses it yet :)

rosefromthedead commented 1 year ago

Yeah, I used to use unreachable!() for these cases, until someone sent me a patch to do it this way. I agree that since match never {} can only be used when it is definitely impossible, it better conveys the semantics of that match arm.

Edit: Good god, I'm never using that email reply thing again. Aside from all the noise it sent and the markdown it didn't apply, this was supposed to be a reply to the thread above :/