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