Skip to content

Generic instance based on ISO 8601 representation #19

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

Closed
wants to merge 1 commit into from

Conversation

parsonsmatt
Copy link
Contributor

This is something I'm really wanting in my own application. I can newtype around, but this seems suitably useful and general that it might want to be included in the date definition.

Another useful instance would be IsForeign -- I can implement and make a PR for that if wanted :)

@@ -36,11 +37,18 @@ foreign import data JSDate :: *
-- | modules.
newtype Date = DateTime JSDate

instance genericDate :: Generic Date where
toSpine d = SString (runFn2 jsDateMethod "toISOString" (toJSDate d))
Copy link
Contributor

Choose a reason for hiding this comment

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

This instance doesn't represent what a Date actually is at runtime, so it's an invalid instance. Foreign data types can't have generic instances for this reason.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So valid instances of Generic have the true representation and not just an isomorphism? That makes sense. I can close the PR if it's nonsensical.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes exactly. If we allowed this, then things like everywhere would apply functions of Strings to Dates which would be bad. This is why it's recommended to derive generic instances, because they're so hard to get right by hand :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ooh, I see! Thanks for the clarification :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for looking at this though. A newtype around String with a smart constructor might be a good way to handle this if a Generic instance is needed for some reason.

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