There is unsurprisingly a run time exception thrown by the following code :
data Necklace = InvalidNecklace |
Necklace { necklace_id :: Int, meow :: Int, ... }
necklace_id InvalidNecklace
Is there some natural way to define a value for necklace_id
when applied to InvalidNecklace
to take a value rather than throwing an exception?
GHC fails with a multiple declarations error for `necklace_id' if I try the obvious thing :
necklace_id InvalidNecklace = -1
Is there perhaps some pragma that'll tell GHC to replace it's inferred declaration by this declaration?
I could declare InvalidNecklace
to be a record by adding { necklace_id :: Int }
, but afaik I cannot guarantee it always returns -1, and generally makes a horrible mess. I could simply define :
get_necklace_id InvalidNecklace = -1
get_necklace_id x = necklace_id x
but this partially defeats the purpose of records.
I suppose one could create a special invalidNecklace
value by writing :
invalidNecklace = Necklace { necklace_id = -1,
meow = error "meow invalidNecklace accessed", ... }
Are there any disadvantages to this second approach? I certainly lose the ability to make meow
strict or unpacked, but perhaps separate debugging and optimized versions could be maintained. Is there a pragma to locally disable warnings for partially initialized records?