lloydmeta / frunk

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

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

Closed rosefromthedead closed 2 years ago

rosefromthedead commented 2 years ago

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

lloydmeta commented 2 years ago

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

ExpHP commented 2 years ago

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

rosefromthedead commented 2 years 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 2 years 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 :/