Are records type safe?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Are records type safe?

Tom Hawkins-2
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
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Are records type safe?

Bulat Ziganshin-2
Hello Tom,

Thursday, May 4, 2006, 5:20:54 PM, you wrote:

> 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?

it shpuld be a runtime check. seems that Haskell is so changed your
mind that you expect that compiler should check anything at compile time
:)

--
Best regards,
 Bulat                            mailto:[hidden email]

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe