Wanted: warning option for usages of unary minus
Isaac Dupree
isaacdupree at charter.net
Sat Apr 7 18:25:22 EDT 2007
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Since the unary negation operator `-' is often considered a wart in
Haskell's syntax and in many cases saying "negate" is arguably clearer
anyway, I propose adding options to GHC to warn about its use. The only
case I don't normally want to be warned about is "negative numeric
literals", e.g. (-1), even if in current Haskell they do mean (negate
(fromInteger 1)) not (fromInteger (negate 1)) (or equivalently,
(fromInteger (negate (fromInteger 1))) as the inner fromInteger === id).
I know this would be useful to someone since I caught myself manually
cleaning some of my code in this way without the help of warning messages :)
Proposed flag names and descriptions (could use a little tidying up) :
- -fwarn-unary-minus
Causes a warning to be emitted for all usages of the unary operator `-',
except those in which its argument is a numeric literal that is not
separated from the minus sign by any space (space includes comments).
- -fwarn-all-unary-minus
For those who hate it with a passion, this option warns about ALL uses
of the unary `-' operator, even code like `(-1)'.
Opinions? (Should I put a feature request in Trac? Is that what I
perhaps should have done in the first place?) Is this simple enough to
try to implement myself so I can start getting to know GHC's internals?
(I suspect that detecting the adjacency of the "-" to a numeric literal
without changing the actual precedence may be a little tricky... -2^6
parses as negate(2^6) and should get a warning, as should (- 2), IMO)
Oh, the actual warning message...
file:line:column: Warning: Unary minus in expression foo
(or, on the next line, In the expression: foo ?)
for an appropriate "foo", which has parentheses added to describe the
effects of operator precedence, as usual for GHC diagnostic messages
(it's just particularly important for making this one easy to understand
in some cases)
Isaac
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFGGBpSHgcxvIWYTTURAt5sAKCEIohHMiuUy9AofhWKzvDOf4rwmgCeJcbj
e2RmW+UX2E6omuLpFfIH9fs=
=1SnR
-----END PGP SIGNATURE-----
More information about the Glasgow-haskell-users
mailing list