Skip to content

Commit 17fdeaa

Browse files
committed
Update example for 02256 with better example
1 parent 3ff08ca commit 17fdeaa

File tree

9 files changed

+42
-41
lines changed

9 files changed

+42
-41
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Accounting where
2+
3+
import qualified AccountingRecords as Personal (PersonalAccount(..))
4+
import qualified AccountingRecords as Business (BusinessAccount(..))
5+
6+
resetPersonalAccount :: Personal.PersonalAccount -> Personal.PersonalAccount
7+
resetPersonalAccount account = account { Personal.balance = 0 }
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{-# LANGUAGE DuplicateRecordFields #-}
2+
module AccountingRecords where
3+
4+
data BusinessAccount = MkBusinessAccount { balance :: Int }
5+
6+
data PersonalAccount = MkPersonalAcccount { balance :: Int }
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{-# LANGUAGE DuplicateRecordFields #-}
2+
module Accounting where
3+
4+
data BusinessAccount = MkBusinessAccount { balance :: Int }
5+
6+
data PersonalAccount = MkPersonalAccount { balance :: Int }
7+
8+
resetPersonalAccount :: PersonalAccount -> PersonalAccount
9+
resetPersonalAccount account = account { balance = 0 }
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: Ambiguous Record Update
3+
---
4+
5+
In this example we declare two records `BusinessAccount` and `PersonalAccount` which each contain a field `balance` which stores the current balance. In the function `resetPersonalAccount` we use this record field to reset the account balance of a personal account to 0.
6+
The compiler can successfully detect that the field `balance` that we mention refers to the field of the record `PersonalAccount` and not to the field of the record `BusinessAccount`, but the mechanism that GHC uses for this is brittle and will be deprecated in a future release.
7+
For this reason GHC emits a warning.
8+
9+
```
10+
before/Accounting.hs:9:42: warning: [GHC-02256] [-Wambiguous-fields]
11+
The record update account
12+
{balance = 0} with type PersonalAccount is ambiguous.
13+
This will not be supported by -XDuplicateRecordFields in future releases of GHC.
14+
|
15+
9 | resetPersonalAccount account = account { balance = 0 }
16+
|
17+
```
18+
19+
In order to make your code compile without warnings you can put the record declarations in a separate module and use qualified imports and names to make it unambiguous which record field you mean.

message-index/messages/GHC-02256/ambiguous/after/AmbiguousA.hs

Lines changed: 0 additions & 7 deletions
This file was deleted.

message-index/messages/GHC-02256/ambiguous/after/AmbiguousB.hs

Lines changed: 0 additions & 7 deletions
This file was deleted.

message-index/messages/GHC-02256/ambiguous/before/AmbiguousA.hs

Lines changed: 0 additions & 9 deletions
This file was deleted.

message-index/messages/GHC-02256/ambiguous/index.md

Lines changed: 0 additions & 17 deletions
This file was deleted.

message-index/messages/GHC-02256/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ flag: -Wambiguous-fields
66
introduced: 9.6.1
77
---
88

9-
With the `DuplicateRecordFields` extension enabled it is possible to use the same field name in multiple records within the same module.
9+
The `DuplicateRecordFields` extension lets you use the same field name in multiple records within the same module.
1010
This can lead to problems when using field accessors to access a field of a record.

0 commit comments

Comments
 (0)