generics-sop
generics-sop copied to clipboard
Generic generic
The reason this is useful is to be able to combine Rep
s of different types, and pretend that they are actually a Generic
type on their own.
nice!
I'd rather do more general instance:
instance (All SListI a, f ~ I) => Generic (SOP f a) where
....
I don't think that Generic (SOP f a)
would make any sense for anything else than f ~ I
.
Done :-)
I've been thinking about this instance in the past, but I've always been hesitating because it isn't actually giving you a generic representation of a SOP. Could you explain your use case in more detail?
My exact use-case: squeal-postgresql
allows users to run insertions of arbitrary types so long as their Rep
matches the expected Rep
for the particular table. However, you don't always actually want your Haskell types to have, e.g., internal IDs in them. That is, if you have a type:
data Location = Location { locType :: Text, locCentre :: Coord, ... }
the corresponding table in your schema may have a serial primary key. With this instance, we can keep the idiomatic Location
type (i.e., don't add a strange "ID" field to it), but still be able to use it for queries/insertions; instead of having traversePrepared_ (insertRow_ #location (Set (param @1)
As #locId :* ...)) [UglyLocation { locId = XXX, ..}]
, we can have traversePrepared_ (insertRow_ #location (Set (param @1)
As #locId :* ...)) [XXX :* from Location {..}]
.
I see. I think I'd feel slightly more comfortable to define a different newtype-wrapper around SOP
and provide the instance for that. This should cover your use case. I'm not sure though. I could certainly be convinced to just accept your PR as it is now. My primary worry that it might become easier to confuse original types with their representations if we add this kind of instance.