From de2c95ce957ec2c30dd7d1bde2eaf248424b41a8 Mon Sep 17 00:00:00 2001 From: Celso Bonutti Date: Thu, 5 Oct 2023 12:14:43 -0300 Subject: [PATCH] adds custom type error for non-derivable instances --- src/Elm/Sorter.hs | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/Elm/Sorter.hs b/src/Elm/Sorter.hs index 5bcfebf..ec625b3 100644 --- a/src/Elm/Sorter.hs +++ b/src/Elm/Sorter.hs @@ -8,6 +8,8 @@ {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeFamilies #-} +{-# LANGUAGE TypeOperators #-} +{-# LANGUAGE UndecidableInstances #-} {-# LANGUAGE NoGeneralizedNewtypeDeriving #-} module Elm.Sorter (Sorter, mkRecordSorter, mkCustom, HasElmSorter (..), render) where @@ -15,7 +17,8 @@ module Elm.Sorter (Sorter, mkRecordSorter, mkCustom, HasElmSorter (..), render) import Data.Generics.Product.Fields (HasField') import Data.Int (Int64) import Data.Proxy -import Data.Text (Text, pack) +import Data.Text (pack) +import qualified Data.Text as T import Elm.Common (letIn) import GHC.Generics import GHC.TypeLits @@ -24,9 +27,9 @@ import Text.PrettyPrint.Leijen.Text hiding ((<$>)) data Sorter = Alphabetical | Increasing - | ByField Text Sorter - | ByNewtype Text Sorter - | Custom Text + | ByField T.Text Sorter + | ByNewtype T.Text Sorter + | Custom T.Text deriving (Eq, Show) render :: Sorter -> Doc @@ -59,6 +62,17 @@ class HasElmSorter a where class GenericElmSorter f where genericElmSorter :: f a -> Sorter +instance + {-# OVERLAPPABLE #-} + ( TypeError + ( Text "ElmSorter only has default instances for newtypes." + :$$: Text "Perhaps you should use `mkRecordSorter` or `mkCustom` instead?" + ) + ) => + GenericElmSorter a + where + genericElmSorter = undefined + instance (HasElmSorter interior, KnownSymbol consName) => GenericElmSorter @@ -134,7 +148,7 @@ instance HasElmSorter Int64 where instance HasElmSorter Double where elmSorter _ = Increasing -instance HasElmSorter Text where +instance HasElmSorter T.Text where elmSorter _ = Alphabetical instance HasElmSorter String where @@ -143,5 +157,5 @@ instance HasElmSorter String where mkRecordSorter :: forall (field :: Symbol) record type_. (HasField' field record type_, KnownSymbol field, HasElmSorter type_) => Proxy record -> Sorter mkRecordSorter _ = ByField (pack $ symbolVal (Proxy :: Proxy field)) (elmSorter (Proxy :: Proxy type_)) -mkCustom :: Text -> Sorter +mkCustom :: T.Text -> Sorter mkCustom = Custom