Skip to content

Instantly share code, notes, and snippets.

@fieldstrength
Last active January 16, 2018 14:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fieldstrength/cb51a61a10aea99b0916a81c8880c13b to your computer and use it in GitHub Desktop.
Save fieldstrength/cb51a61a10aea99b0916a81c8880c13b to your computer and use it in GitHub Desktop.
{-# 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