Created
October 6, 2018 03:55
-
-
Save naoto-ogawa/2f598c9b7bf47b2ac42e1d38da210b04 to your computer and use it in GitHub Desktop.
Duplicate Record Fields in Haskell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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