title: Are there default values for record getters in Haskell?
body: <p>There is unsurprisingly a run time exception thrown by the following code :</p>
  
  <pre><code>data Necklace = InvalidNecklace |
      Necklace { necklace_id :: Int, meow :: Int, ... }
  necklace_id InvalidNecklace
  </code></pre>
  
  <p>Is there some natural way to define a value for <code>necklace_id</code> when applied to <code>InvalidNecklace</code> to take a value rather than throwing an exception? </p>
  
  <p>GHC fails with a multiple declarations error for `necklace_id' if I try the obvious thing :</p>
  
  <pre><code>necklace_id InvalidNecklace = -1
  </code></pre>
  
  <p>Is there perhaps some pragma that'll tell GHC to replace it's inferred declaration by this declaration?</p>
  
  <p>I could declare <code>InvalidNecklace</code> to be a record by adding <code>{ necklace_id :: Int }</code>, but afaik I cannot guarantee it always returns -1, and generally makes a horrible mess.  I could simply define :</p>
  
  <pre><code>get_necklace_id InvalidNecklace = -1
  get_necklace_id x = necklace_id x
  </code></pre>
  
  <p>but this partially defeats the purpose of records. </p>
  
  <p>I suppose one could create a special <code>invalidNecklace</code> value by writing :</p>
  
  <pre><code>invalidNecklace = Necklace { necklace_id = -1,
       meow = error "meow invalidNecklace accessed", ... }
  </code></pre>
  
  <p>Are there any disadvantages to this second approach?  I certainly lose the ability to make <code>meow</code> 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?</p>