Skip to content

DuplicateRecordFields for different constructors in one type

Summary

DuplicateRecordFields doesn't work for different constructors in the same data type as well as it works for different types.

Steps to reproduce

This code works:

{-# LANGUAGE DuplicateRecordFields #-}

data A = A{f :: Int}
  deriving Show

data B = B{f :: String}
  deriving Show

a = A{f = 10}

b = B{f = "foo"}

-- >>> (a, b)
-- <<< (A {f = 10},B {f = "foo"})

A{f = a'} = a

B{f = b'} = b

-- >>> (a', b')
-- <<< (10,"foo")

But this doesn't:

data AB = A{f :: Int} | B{f :: String}
a.hs:(4,1)-(5,15): error:
    • Constructors A and B give different types for field ‘f’
    • In the data type declaration for ‘AB’
  |
4 | data AB = A{f :: Int} | B{f :: String}
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...

Expected behavior

The same:

a = A{f = 10}

b = B{f = "foo"}

-- >>> (a, b)
-- <<< (A {f = 10},B {f = "foo"})

A{f = a'} = a

B{f = b'} = b

-- >>> (a', b')
-- <<< (10,"foo")

Environment

  • GHC version used: The Glorious Glasgow Haskell Compilation System, version 8.10.1
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information