Open deepfire opened 8 years ago
Couldn't you use GeneralizedNewtypeDeriving
to automate deriving the instance?
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
newtype Foo = Foo { getFoo :: Bar } deriving (ParseRecord)
Gabriel Gonzalez notifications@github.com writes:
Couldn't you use GeneralizedNewtypeDeriving to automate deriving the instance?
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
newtype Foo = Foo { getFoo :: Bar } deriving (ParseRecord)
This almost works, indeed:
newtype PAlias = PAlias { from ∷ String }
deriving ( -- unrelated instances
Generic, Show
-- instances for optparse-generic
, ParseRecord, ParseFields, ParseField, Read)
data Params where
Params
∷ {
project_id ∷ PAlias
} → ReportParams
deriving (Generic, Show)
instance ParseRecord ReportParams
However:
--project_id PALIAS
..instead of:
--project_id STRING
{-# LANGUAGE Arrows #-} {-# LANGUAGE AllowAmbiguousTypes #-} {-# LANGUAGE ApplicativeDo #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE DeriveAnyClass #-} {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE DuplicateRecordFields #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE KindSignatures #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE RecordWildCards #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE StandaloneDeriving #-} {-# LANGUAGE TypeApplications #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE UndecidableInstances #-} {-# LANGUAGE UndecidableSuperClasses #-} {-# LANGUAGE UnicodeSyntax #-}
In the worst case you can just hand-write the instance, which would be:
instance ParseField PAlias where
parseField m = fmap PAlias (parseField m)
How does one provide for using newtypes as option record types?
Currently the failure mentions missing
ParseFields
instance, but when one tries to provide a hand-written one, along the lines of the existingParseField
instances, one faces the fact thatparseString
is not exported.Are there plans to automate instances for newtypes? Or to document how to define
ParseFields
instances for own types?