application of the record field may fail
Johannes Waldmann <[email protected]> Fri, 9 May 2025 15:54:48 +0200
| Newsgroups | gmane.comp.lang.haskell.cafe |
|---|---|
| Message-ID | <[email protected]> |
Dear Cafe,
(ignore following email, but perhaps the observation is still useful,
the answer is to use `-Weverything`, which gives more than `-Wall`. Obviously.)
For two constructors with fields of identical type but different name,
data T = A {foo::()} | B {f00 ::()}
GHC detects a typo in the field name
ghci> f x = case x of A{f00 = f} -> f ; B {} -> ()
<interactive>:2:19: error: [GHC-53822]
• Constructor ‘A’ does not have field ‘f00’
Good. Now changing the program slightly,
ghci> f x = case x of A{} -> f00 x ; B {} -> ()
the program becomes statically correct.
Can we get a warning here? (to detect the typo).
This example is close to real code,
not foo and f00 but something_longer and something_longerr.
Yes I shoudn't do this, and make names more descriptive/distinct.
And types! It's only a typo because it's not a type error.
[EDIT] the answer is yes, we can, with -Weverything,
<interactive>:9:24: warning: [GHC-17335] [-Wincomplete-record-selectors]
The application of the record field ‘f00’ may fail for the following constructors: A
this sounds over-cautious, because it _will_ fail (if evaluated at all).
- J.W.
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.