Skip to content

Conversation

arthurxavierx
Copy link

Polymorphic Variants are useful for modelling route schemes that follow a common API but that may be expressed partially for different resources in an application. This is where Generic based CRUD routes fall short, for example.

The API proposes for Variant codecs follows that of record and prop.


I'm not totally sure about the names I chose for vcase and %=. Happy to take suggestions on those :)

-- | # vcase (SProxy :: _ "new") (path "new" $ pure unit)
-- | ```
variant :: forall r. RouteDuplex r (Variant ())
variant = RouteDuplex mempty (Chomp \_ -> Fail EndOfPath)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if Chomp \_ -> Fail EndOfPath is the best way to have the codec fail here.

@arthurxavierx
Copy link
Author

Oh nice! I didn't know that one, thanks for pointing it out @srghma :)

I actually think that the functions provided in this module are more powerful because they account for ordering, while the approach of paluh/purescript-routing-duplex-variant uses a Record argument to match against, which may cause unexpected results depending on how you need to sort the routes.

@natefaubion
Copy link
Owner

I'd prefer to have both options if possible. We use the record approach for Generic-based sum because it's so much nicer than using SProxies. I like having the combinator approach as the fundamentals, and then record syntax as sugar (like in purescript-variant with on vs match).

@srghma srghma mentioned this pull request Jul 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants