Last active
January 16, 2018 14:45
-
-
Save fieldstrength/cb51a61a10aea99b0916a81c8880c13b to your computer and use it in GitHub Desktop.
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 DataKinds #-} | |
{-# LANGUAGE DuplicateRecordFields #-} | |
{-# LANGUAGE FlexibleContexts #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
{-# LANGUAGE MultiParamTypeClasses #-} | |
{-# LANGUAGE OverloadedLabels #-} | |
{-# LANGUAGE ScopedTypeVariables #-} | |
{-# LANGUAGE TypeApplications #-} | |
{-# OPTIONS_GHC -fno-warn-orphans #-} | |
module Rec where | |
import GHC.OverloadedLabels | |
import GHC.Records | |
instance HasField s r a => IsLabel s (r -> a) where | |
fromLabel = getField @s | |
data Person = Person { name :: String, age :: Int } | |
data Group = Group { name :: String, members :: [Person] } | |
spyro :: Person | |
spyro = Person "Spyro" 7 | |
hisName :: String | |
hisName = #name spyro | |
-- now DataKinds, FlexibleContexts needed | |
getName :: HasField "name" r String => r -> String | |
getName = #name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment