Skip to content

Instantly share code, notes, and snippets.

@codedmart
Created November 21, 2022 15:26
Show Gist options
  • Save codedmart/c7e2fa0b91fc58cae12200d431dc0b07 to your computer and use it in GitHub Desktop.
Save codedmart/c7e2fa0b91fc58cae12200d431dc0b07 to your computer and use it in GitHub Desktop.
newtype FooBar (names :: [(Symbol, id)]) a =
FooBar { unFooBar :: a }
instance
( Show a
) => HasId (FooBar '[] a)
where
type UniqId (FooBar '[] a) = ()
mkId (FooBar _) = ()
instance
( Show a
, HasField' name a id
, Show id
, KnownSymbol name
) => HasId (FooBar '[ '(name, id)] a)
where
type UniqId (FooBar '[ '(name, id)] a) = id
mkId (FooBar a) = getField @name a
instance
( Show a
, HasField' name1 a id1
, Show id1
, KnownSymbol name1
, HasField' name2 a id2
, Show id2
, KnownSymbol name2
) => HasId (FooBar '[ '(name1, id1), '(name2, id2)] a)
where
type UniqId (FooBar '[ '(name1, id1), '(name2, id2)] a) = (id1, id2)
mkId (FooBar a) = (getField @name1 a, getField @name2 a)
instance
( Show a
, HasField' name1 a id1
, Show id1
, KnownSymbol name1
, HasField' name2 a id2
, Show id2
, KnownSymbol name2
, HasField' name3 a id3
, Show id3
, KnownSymbol name3
) => HasId (FooBar '[ '(name1, id1), '(name2, id2), '(name3, id3)] a)
where
type UniqId (FooBar '[ '(name1, id1), '(name2, id2), '(name3, id3)] a) = (id1, id2, id3)
mkId (FooBar a) = (getField @name1 a, getField @name2 a, getField @name3 a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment