Last active
October 22, 2019 19:31
-
-
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
This file contains hidden or 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 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