Not surprisingly, a runtime exception is thrown by the following code:
data Necklace = InvalidNecklace | Necklace { necklace_id :: Int, meow :: Int, ... } necklace_id InvalidNecklace
Is there any natural way to define a value for necklace_id when applied to InvalidNecklace to accept a value rather than throw an exception?
The GHC fails with a multiple declaration error for `necklace_id 'if I try to do the obvious thing:
necklace_id InvalidNecklace = -1
Is there some kind of pragma that will tell the GHC to replace its proposed announcement with this announcement?
I could declare InvalidNecklace record by adding { necklace_id :: Int } , but afaik I can’t guarantee that it always returns -1, and usually makes a terrible mess. I could just determine:
get_necklace_id InvalidNecklace = -1 get_necklace_id x = necklace_id x
but this partially violates the purpose of the records.
I believe that you can create a special InvalidNecklace value by writing:
invalidNecklace = Necklace { necklace_id = -1, meow = error "meow invalidNecklace accessed", ... }
Are there any flaws in this second approach? Of course, I lose the ability to make meow strict or unpacked, but perhaps you can save separate debugging and optimized versions. Is there a pragma for locally disabling alerts for partially initialized entries?