Last active
October 4, 2023 20:30
-
-
Save danidiaz/1659defe71d5d51ae042e001c98014ae to your computer and use it in GitHub Desktop.
record update pain
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 DuplicateRecordFields #-} | |
{-# LANGUAGE NoFieldSelectors #-} | |
{-# LANGUAGE OverloadedRecordDot #-} | |
{-# LANGUAGE RecordWildCards #-} | |
module Main where | |
import Distribution.Simple (KnownExtension(RecordWildCards)) | |
data Foo = Foo { | |
aaa :: Int, | |
bbb :: Bool | |
} | |
data Bar = Bar { | |
aaa :: Int, | |
bbb :: Bool | |
} | |
foo :: Foo | |
foo = Foo 5 True | |
bar :: Bar | |
bar = Bar 3 False | |
wee :: Foo -> Bar | |
wee Foo {aaa,bbb} = Bar {aaa,bbb} | |
-- | Ambiguous | |
-- boo' :: Foo -> Bar -> Bar | |
-- boo' foo bar = bar { aaa = foo.aaa } | |
-- | Using the explicit constructor + RecordWildCards is not ambiguous | |
-- | |
-- https://downloads.haskell.org/ghc/latest/docs/users_guide/exts/duplicate_record_fields.html | |
-- > Uses of fields that are always unambiguous because they mention the | |
-- constructor, including construction and pattern-matching, may freely use | |
-- duplicated field names. | |
-- | |
boo :: Foo -> Bar -> Bar | |
boo foo Bar {aaa,..} = Bar { aaa = foo.aaa, .. } | |
main :: IO () | |
main = pure () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment