
#16049: Add a warning flag that warns when a datatype could be a newtype -------------------------------------+------------------------------------- Reporter: chessai | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: 8.8.1 Component: Compiler | Version: 8.6.3 Resolution: | Keywords: flags Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Description changed by chessai: Old description:
More often than not, `newtype` is desirable over `data` if a type has a single field. I propose a warning flag `-Wdata-could-be-newtype` that would warn users if a data type they've defined has a single field (and thus could be a `newtype`). It would need to be explicitly enabled (and of course enabled by `-Weverything`). An example (relevant Trac ticket: #15995):
{{{#!hs -- Control.Concurrent.QSem:
data QSem = QSem !(MVar (Int, [MVar ()], [MVar ()]))
}}}
There's a bang pattern on its single field, but this could just be supplied at every pattern-matching site of `QSem`.
New description: More often than not, `newtype` is desirable over `data` if a type has a single field. I propose a warning flag `-Wdata-could-be-newtype` that would warn users if a data type they've defined has a single field (and thus could be a `newtype`). It would need to be explicitly enabled (and of course enabled by `-Weverything`). An example (relevant Trac ticket: #15995): {{{#!hs -- Control.Concurrent.QSem: data QSem = QSem !(MVar (Int, [MVar ()], [MVar ()])) }}} could be a newtype {{{#!hs -- Control.Concurrent.QSem: newtype QSem = QSem (MVar (Int, [MVar ()], [MVar ()])) }}} -- -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/16049#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler