Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Introduce As typeclass: composable errors #38

Open
chshersh opened this issue Oct 1, 2019 · 2 comments
Open

[RFC] Introduce As typeclass: composable errors #38

chshersh opened this issue Oct 1, 2019 · 2 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@chshersh
Copy link
Contributor

chshersh commented Oct 1, 2019

I propose to introduce the following typeclass:

class As s a where
    as    :: a -> s
    match :: s -> Maybe a

It represents the ability to convert errors of type a to errors of type s and match back. Obvious law: match . as = Just. It can be used not only for errors, but this is its main goal.

So the type signature of a function like this:

queryNamed
    :: (WithError PgNamedError m, WithDb env m, FromRow res)
    => Sql.Query
    -> [NamedParam]
    -> m [res]

will become

queryNamed
    :: (WithError err m, As err PgNamedError, WithDb env m, FromRow res)
    => Sql.Query
    -> [NamedParam]
    -> m [res]

with a trivial instance for PgNamedError:

instance As PgNamedError PgNamedError where
    as    = id
    match = Just
@chshersh chshersh added enhancement New feature or request question Further information is requested labels Oct 1, 2019
@vrom911
Copy link
Member

vrom911 commented Oct 1, 2019

I see how helpful it could be with our architecture. Also, having possible errors that function can throw on type-level is also sounds good 👍

@chshersh
Copy link
Contributor Author

@vrom911 I believe this approach can be implemented now and our projects still can use monomorphic PgNamedError without need to migrate to this approach. What do you think, should we do it now and use it later when we're ready?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants