Skip to content

Newtype Deriving - #2304

Merged
paf31 merged 3 commits into
masterfrom
gnd
Sep 16, 2016
Merged

Newtype Deriving#2304
paf31 merged 3 commits into
masterfrom
gnd

Conversation

@paf31

@paf31 paf31 commented Sep 11, 2016

Copy link
Copy Markdown
Contributor

This adds newtype instance syntax, which implements instances for newtypes by simply casting the corresponding dictionary for the type under the newtype.

@paf31

paf31 commented Sep 11, 2016

Copy link
Copy Markdown
Contributor Author

Fixes #514

@garyb

garyb commented Sep 11, 2016

Copy link
Copy Markdown
Member

I'll review this properly shortly, but I have a minor syntax comment: I think it's a little weird to start the declaration with newtype. I don't think we overload "keywords" quite in that way anywhere else so it sticks out to me a bit.

derive newtype instance perhaps? (I assume derive instance is reserved for cases where an instance is wanted for the type directly, like Generic?)

@garyb

garyb commented Sep 11, 2016

Copy link
Copy Markdown
Member

Actually, that distinction probably shouldn't be allowed/necessary, should it? The newtype version of a Generic instance wouldn't behave correctly. In that case maybe the derive instance syntax can just work?

@paf31

paf31 commented Sep 11, 2016

Copy link
Copy Markdown
Contributor Author

I think there needs to be some distinction, since for example, derived Eq and Ord are not the same as newtype-derived Eq and Ord.

@garyb

garyb commented Sep 12, 2016

Copy link
Copy Markdown
Member

Well, I was thinking about that too. Their implementation differs, but are they not the same? I would assume the derived instance would be along the lines of:

newtype Foo = Foo String

instance eqFoo :: Eq Foo where
  eq (Foo a) (Foo b) = eq a b

Which when you remove the newtype unwrapping is identical?

@paf31

paf31 commented Sep 12, 2016

Copy link
Copy Markdown
Contributor Author

Oh right, that's true. I was thinking the derived instances would compare the deep structure of the two values, but that's not right.

I'm fine with derive newtype instance ... by the way.

@paf31

paf31 commented Sep 12, 2016

Copy link
Copy Markdown
Contributor Author

@garyb Updated, how's this? I'd prefer to keep the syntax for derived newtype instances separate for now, since we don't know if all future derivable instances will be the same as their newtype-derived counterparts.


forValues (TypeClassDictionary c _ _) = mconcat (map f (constraintArgs c))
forValues (SuperClassDictionary _ tys) = mconcat (map f tys)
forValues (DeferredDictionary _ tys) = mconcat (map f tys)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Isn't this just foldMap btw?

@garyb

garyb commented Sep 12, 2016

Copy link
Copy Markdown
Member

I'd prefer to keep the syntax for derived newtype instances separate for now

Fair enough. Should we explicitly disallow it for Generic though?

@paf31

paf31 commented Sep 12, 2016

Copy link
Copy Markdown
Contributor Author

Should we explicitly disallow it for Generic though?

I'm not sure. The instances for Generic are technically invalid, but I can see a case for them. Consider something like deriving a JSON parser for example. You want the parser to work on the newtype too, but want the newtype for the distinction at the type level. This seems somehow "less wrong" than a Generic instance where the representations are not isomorphic, like an instance for Date which coerces to an integer.

I'd rather not make a special case for any particular class actually.

@garyb

garyb commented Sep 12, 2016

Copy link
Copy Markdown
Member

My worry was that fromSpine <<< toSpine would fail for a newtype-derived Generic, but actually, it wouldn't would it? I think I was just being overly concerned.

@garyb

garyb commented Sep 16, 2016

Copy link
Copy Markdown
Member

:shipit: 🎆

@paf31
paf31 merged commit ebd7c3c into master Sep 16, 2016
@paf31
paf31 deleted the gnd branch September 16, 2016 00:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants