[Haskell-cafe] Are records type safe?

Tom Hawkins tomahawkins at gmail.com
Thu May 4 09:20:54 EDT 2006


I was experimenting with records this morning and discovered a slight oddity...

data Rec = RecA { a :: Int } | RecB { a :: Int, b :: Bool } deriving Show

main :: IO ()
main = do
  print (b (RecB { a = 1, b = True }))  -- Works
  print (b (RecA { a = 1 }))            -- Doesn't

Of course extracting "b" from a record constructed with "RecA" is
illegal, but the compiler did not warn against it (ghc -W).  I was
expecting a warning similar to non-exhaustive pattern matching.

Are records with optional fields really type safe?

-Tom


More information about the Haskell-Cafe mailing list