Skip to content

Instantly share code, notes, and snippets.

@friedbrice
Last active June 28, 2019 05:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save friedbrice/d88a3eb6bd44cb93aab0aa610eb42f6b to your computer and use it in GitHub Desktop.
Save friedbrice/d88a3eb6bd44cb93aab0aa610eb42f6b to your computer and use it in GitHub Desktop.
tidy-records
module App where
import Labels ()
import Types
import Data.Generics.Product.Fields
main :: IO ()
main = do
let
a = A { this = "this", that = 4, other = 3.2 }
b = B { other = "other", wise = 2.7 }
c1 = C1 { id = 3, this = "self", that = 2.41 }
c2 = C2 { id = 4, self = 0.717, other = "that" }
print (#this a)
print (#that a)
print (#other a)
print (#other b)
print (#wise b)
-- print (#this b) -- type error (as desired)
print (#id c1)
-- print (#this c1) -- type error (as desired)
-- print (#that c1) -- type error (as desired)
print (#id c2)
-- print (#self c2) -- type error (as desired)
-- print (#other c2) -- type error (as desired)
return ()
getId :: HasField' "id" s a => s -> a
getId = #id
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Labels where
import Data.Generics.Product.Fields
import Lens.Micro.Extras
import GHC.OverloadedLabels
instance HasField' l s a => IsLabel l (s -> a) where
fromLabel = view (field' @l)
import Distribution.Simple
main = defaultMain
name: tidy-records
version: 0.1.0.0
build-type: Simple
cabal-version: >=1.10
library
default-language: Haskell2010
ghc-options: -Wall -Werror
hs-source-dirs: .
exposed-modules: App
, Types
, Labels
default-extensions: DataKinds
, DeriveGeneric
, DuplicateRecordFields
, FlexibleContexts
, FlexibleInstances
, MonoLocalBinds
, MultiParamTypeClasses
, ScopedTypeVariables
, TypeApplications
, OverloadedLabels
build-depends: base >=4.12 && <5
, generic-lens
, microlens
module Types where
import GHC.Generics
data A
= A { this :: String, that :: Int, other :: Double }
deriving Generic
data B
= B { other :: String, wise :: Double }
deriving Generic
data C
= C1 { id :: Int, this :: String, that :: Double }
| C2 { id :: Int, self :: Double, other :: String }
deriving Generic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment