Skip to content

Instantly share code, notes, and snippets.

@danidiaz
Last active October 22, 2019 19:31
Show Gist options
  • Save danidiaz/72d06562ae4811ef65ea20989ac7ae3b to your computer and use it in GitHub Desktop.
Save danidiaz/72d06562ae4811ef65ea20989ac7ae3b to your computer and use it in GitHub Desktop.
record accessor shenanigans inspired by https://github.com/ghc-proposals/ghc-proposals/pull/282
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module Main where
import GHC.OverloadedLabels
import GHC.Records
import Prelude hiding ((/))
newtype Access r v = Access (r -> v)
instance HasField label r v => IsLabel label (Access r v) where
fromLabel = Access (getField @label)
(/) :: r -> Access r v -> v
(/) r (Access f) = f r
data Person = Person {name :: String, address :: Address}
data Address = Address {street :: String, number :: Int}
main :: IO ()
main = do
let person = Person { name = "foo", address = Address "bar" 3 }
print $ person / #address / #street
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment