Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds custom type error for non-derivable instances of HasElmSorter #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions src/Elm/Sorter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE NoGeneralizedNewtypeDeriving #-}

module Elm.Sorter (Sorter, mkRecordSorter, mkCustom, HasElmSorter (..), render) where

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
Expand All @@ -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
Expand Down Expand Up @@ -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

Comment on lines +65 to +75
Copy link
Member

Choose a reason for hiding this comment

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

this seems reasonable as long as this is the lowest-possible priority instance (which it should be, seeing as it's as general as possible)

Copy link
Member Author

Choose a reason for hiding this comment

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

It is, yes

instance
(HasElmSorter interior, KnownSymbol consName) =>
GenericElmSorter
Expand Down Expand Up @@ -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
Expand All @@ -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