
26 May
2012
26 May
'12
8:14 p.m.
Is it any more ridiculous than
f x@Nothing {} = fromJust x main = print (f Nothing)
crashing at run time? That is what you are expressing with your first one. This issue is completely unrelated to the named field syntax, they behave exactly like data types with non-named fields. However, you can achieve something like what you want with phantom types.
data ALike data BLike
data MyData t = A {a::Int, b::Int} | B {c::Int}
mkA x y = A x y :: MyData ALike mkB x = B x :: MyData BLike
then you can write functions of 'MyData ALike' to indicate it will only have 'A' as a constructor 'MyData BLike' to indicate it will only have 'B' and 'forall t . MyData t' for functions that can take a general MyData that can have either. John