From 3670ee0207749da1934c96451b802da636149d6e Mon Sep 17 00:00:00 2001 From: David Feuer Date: Thu, 9 Jun 2022 01:05:43 -0400 Subject: [PATCH 1/2] Remove impossible case in generic instance Remove the custom code for the impossible case of an infix constructor that doesn't have exactly two fields. --- lib/Fmt/Internal/Generic.hs | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/lib/Fmt/Internal/Generic.hs b/lib/Fmt/Internal/Generic.hs index 30efe94..2aa56e3 100644 --- a/lib/Fmt/Internal/Generic.hs +++ b/lib/Fmt/Internal/Generic.hs @@ -108,22 +108,17 @@ instance (GetFields a, Constructor c) => GBuildable (M1 C c a) where -- * Ordinarily e.g. "Foo" is prefix and e.g. ":|" is infix -- * However, "Foo" can be infix when defined as "a `Foo` b" -- * And ":|" can be prefix when defined as "(:|) a b" - gbuild c@(M1 x) = case conFixity c of - Infix _ _ - | [a, b] <- fields -> format "({} {} {})" a infixName b - -- this case should never happen, but still - | otherwise -> format "<{}: {}>" - prefixName - (mconcat (intersperse ", " fields)) - Prefix - | isTuple -> tupleF fields - | conIsRecord c -> nameF (build prefixName) (blockMapF fieldsWithNames) - | null (getFields x) -> build prefixName - -- I believe that there will be only one field in this case - | null (conName c) -> mconcat (intersperse ", " fields) - | otherwise -> format "<{}: {}>" - prefixName - (mconcat (intersperse ", " fields)) + gbuild c@(M1 x) + | Infix _ _ <- conFixity c + , [a, b] <- fields = format "({} {} {})" a infixName b + | isTuple = tupleF fields + | conIsRecord c = nameF (build prefixName) (blockMapF fieldsWithNames) + | null (getFields x) = build prefixName + -- I believe that there will be only one field in this case + | null (conName c) = mconcat (intersperse ", " fields) + | otherwise = format "<{}: {}>" + prefixName + (mconcat (intersperse ", " fields)) where (prefixName, infixName) | ":" `isPrefixOf` conName c = ("(" ++ conName c ++ ")", conName c) From 78bdff9c8adf8d37eb7c44feaaf1278c5dfd0443 Mon Sep 17 00:00:00 2001 From: David Feuer Date: Thu, 9 Jun 2022 01:09:21 -0400 Subject: [PATCH 2/2] Comment --- lib/Fmt/Internal/Generic.hs | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/Fmt/Internal/Generic.hs b/lib/Fmt/Internal/Generic.hs index 2aa56e3..197aec2 100644 --- a/lib/Fmt/Internal/Generic.hs +++ b/lib/Fmt/Internal/Generic.hs @@ -110,6 +110,7 @@ instance (GetFields a, Constructor c) => GBuildable (M1 C c a) where -- * And ":|" can be prefix when defined as "(:|) a b" gbuild c@(M1 x) | Infix _ _ <- conFixity c + -- There will always be two fields in this context. , [a, b] <- fields = format "({} {} {})" a infixName b | isTuple = tupleF fields | conIsRecord c = nameF (build prefixName) (blockMapF fieldsWithNames)