Last active
February 22, 2016 22:57
-
-
Save danidiaz/3b9a6865686c777f328c to your computer and use it in GitHub Desktop.
Experiments with OverloadedLabels
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 OverloadedLabels #-} | |
{-# LANGUAGE DataKinds #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
{-# LANGUAGE MultiParamTypeClasses #-} | |
module Main where | |
import GHC.OverloadedLabels | |
data Foo = Foo | |
{ | |
_bar :: String | |
, _baz :: Int | |
} | |
-- https://ghc.haskell.org/trac/ghc/wiki/Records/OverloadedRecordFields/OverloadedLabels | |
-- | |
-- *Main> :t #baz | |
-- #baz :: IsLabel "baz" t => t | |
instance IsLabel "baz" (Foo -> Int) where | |
fromLabel _ = _baz | |
instance IsLabel "odd" (Foo -> Int) where | |
fromLabel _ = _baz | |
instance IsLabel "odd" (Foo -> String) where | |
fromLabel _ = _bar | |
fooval :: Foo | |
fooval = Foo "foo" 5 | |
main :: IO () | |
main = do | |
print (#baz fooval :: Int) | |
-- The following is a bit odd | |
print (#odd fooval :: Int) | |
print (#odd fooval :: String) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment