-
Notifications
You must be signed in to change notification settings - Fork 28
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
Explain how to write Show instances #221
Comments
Look ma, no
Doesn't work:
And now it does!
And
|
Interesting, I thought it was somewhere in the docs, but apparently it never went beyond Slack discussions.
Looks like a good place (in README) to write how one can do it not easily (but still it looks quite easy). Btw, we've been doing |
Problem: `Container` typeclass contains several unsafe functions which assume that list if not empty - `foldr1`, `foldl1`, `minimum` and `maximum`. Since universum mostly aimed to be used in business logic, it would be nice to avoid such unsafe methods and make user ensure explicitly that list is not empty. Solution: as we did with `head`, we turn those methods into safe variants returning `Maybe`. In next commits I will also add versions which work with `NonEmpty` and unsafe versions. We could provide potentially more efficient implementation for our new safe methods in `instance Container NonEmpty`, but I don't do this for now as it requires some benchmarking.
Problem: sometimes we know in types that using `foldr1` or `maximum` is safe, would be nice to have a function which does not return `Maybe`. Solution: as we did for `head`, here I add versions of those and similar functions which work with `NonEmpty`.
Problem: sometimes user is completely sure that his structure is not empty, he may want to use the original `foldr1` and `maximum` from `Data.List`. Solution: make those and similar functions be provided in `Unsafe` module.
Problem: sometimes we know in types that using `foldr1` or `maximum` is safe, would be nice to have a function which does not return `Maybe`. Solution: as we did for `head`, here I add versions of those and similar functions which work with `NonEmpty`.
Problem: sometimes user is completely sure that his structure is not empty, he may want to use the original `foldr1` and `maximum` from `Data.List`. Solution: make those and similar functions be provided in `Unsafe` module.
Problem: `Container` typeclass contains several unsafe functions which assume that list if not empty - `foldr1`, `foldl1`, `minimum` and `maximum`. Since universum mostly aimed to be used in business logic, it would be nice to avoid such unsafe methods and make user ensure explicitly that list is not empty. Solution: as we did with `head`, we turn those methods into safe variants returning `Maybe`. In next commits I will also add versions which work with `NonEmpty` and unsafe versions. We could provide potentially more efficient implementation for our new safe methods in `instance Container NonEmpty`, but I don't do this for now as it requires some benchmarking.
Problem: sometimes we know in types that using `foldr1` or `maximum` is safe, would be nice to have a function which does not return `Maybe`. Solution: as we did for `head`, here I add versions of those and similar functions which work with `NonEmpty`.
Problem: sometimes user is completely sure that his structure is not empty, he may want to use the original `foldr1` and `maximum` from `Data.List`. Solution: make those and similar functions be provided in `Unsafe` module.
May you and your kin prosper for a thousand years |
@Martoon-00 did you close it by mistake? |
Oh |
Another reason for it is that if you use I guess I have spare 10 minutes to do this issue, a PR should come shortly. |
Problem: if you manually define `Show` instance and switch to `Universum`, they will fail to compile. It's a gotcha we don't mention. Solution: mention it in gotchas and the migration guide.
Problem: if you manually define `Show` instance and switch to `Universum`, they will fail to compile. It's a gotcha we don't mention. Solution: mention it in gotchas and the migration guide.
:nate: #228 |
Yep, sorry :/ |
[#221] Explain how to write Show instances
A workaround I found: if you add
import qualified Prelude
to your module, suddenly you can writeShow
instances as usual.The text was updated successfully, but these errors were encountered: