[Haskell-cafe] Record syntax, reopening a can of worms.

timothyhobbs at seznam.cz timothyhobbs at seznam.cz
Sun May 27 00:28:29 CEST 2012


Hello list,

As my first post.  I'd like to open a can of worms that I sure has been 
opened before.  That is record syntax.

As we all know, there are some type safety problems with our current record 
syntax. The most obvious is that this compiles without even giving you a 
warning:

>data MyData = 
> A {a::Int,
>    b::Int} |
> B {c::Int}

>foo :: MyData -> Int
>foo a at A{} = c a

>main :: IO ()
>main = print $ foo (A 1 2)

This compiles, then crashes at run time.  Rediculus, why didn't the glorious
GHC catch this?  This is not some ambigious scenario...  One way I found to 
improve type checking is this:

>data MyData = A A' | B B'

>data A' = A'{a::Int,
>             b::Int}
>data B' = B'{c::Int}

>foo :: MyData -> Int
>foo (A a) = c a 

>main :: IO ()
>main = print $ foo (A (A' 1 2))

This doesn't compile anymore because a :: A' and c :: B' -> X

It seems like the later method is simply better.  Better type checking = 
fewer runtime errors(I actually ran into a crash in a real project because 
of this lack of proper type checking...).

Can we have a nice sugar for doing the later method?

Thank you for your time,
Timothy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20120527/9d4ec26e/attachment.htm>


More information about the Haskell-Cafe mailing list