Skip to content

Instantly share code, notes, and snippets.

@danidiaz
Last active October 4, 2023 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danidiaz/1659defe71d5d51ae042e001c98014ae to your computer and use it in GitHub Desktop.
Save danidiaz/1659defe71d5d51ae042e001c98014ae to your computer and use it in GitHub Desktop.
record update pain
{-# 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