Skip to content

Instantly share code, notes, and snippets.

@naoto-ogawa
Created October 6, 2018 03:55
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 naoto-ogawa/2f598c9b7bf47b2ac42e1d38da210b04 to your computer and use it in GitHub Desktop.
Save naoto-ogawa/2f598c9b7bf47b2ac42e1d38da210b04 to your computer and use it in GitHub Desktop.
Duplicate Record Fields in Haskell
{-# LANGUAGE
DuplicateRecordFields
, OverloadedLabels
, DataKinds
, TypeFamilies
, FlexibleInstances
, MultiParamTypeClasses
#-}
import GHC.OverloadedLabels
data Foo = Foo { same :: Int }
data Bar = Bar { same :: Float }
data Bazz = Bazz { same :: Int }
instance (a ~ Int) => IsLabel "same" (Foo -> a) where
fromLabel (Foo x) = x
instance (a ~ Float) => IsLabel "same" (Bar -> a) where
fromLabel (Bar x) = x
instance (a ~ Int) => IsLabel "same" (Bazz -> a) where
fromLabel (Bazz x) = x
main :: IO ()
main = do
let x = Foo 1
let y = Bar 2
let z = Bazz 3
print $ #same x
print $ #same y
print $ #same z
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment