Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7172247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:34:39+00:00 2026-05-28T15:34:39+00:00

There is unsurprisingly a run time exception thrown by the following code : data

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T15:34:41+00:00Added an answer on May 28, 2026 at 3:34 pm

    (UPDATED BELOW)

    As you discovered, the getter defined by the Necklace declaration cannot be further defined. There is no pragma to change this.

    The common practice in Haskell would be to use this style:

    get_necklace_id :: Necklace -> Maybe Int
    get_necklace_id InvalidNecklace = Nothing
    get_necklace_id (Necklace x) = Just x
    

    Using a magic return value of “-1” is common style in C-type languages with simpler type systems. But note that Maybe Int is isomorphic to Necklace, so it adds little in the simplest case (except access to the large number of common functions for handling Maybe that may not exist for Necklace). If you make Necklace more complicated then get_necklace_id makes sense.

    For larger projects it is possible to have template Haskell or an extra tool automatically create the get_necklace_id above.

    UPDATE: Using fromJust is not a particularly good idea. To get “reasonable defaults” and “no failure modes” you may compose the get_necklace_id :: Necklace -> Maybe Int with Data.Maybe.fromMaybe :: a -> Maybe a -> a (one of the common Maybe handling functions) like this:

    from_necklace_id :: Int -> Necklace -> Int
    from_necklace_id default = fromMaybe default . get_necklace_id
    
    a_necklace_id :: Necklace -> Int
    a_necklace_id = from_necklace_id (-1)
    

    The a_necklace_id is identical to your function that replaces InvalidNecklace with (-1). Code that needs a different default can use from_necklace_id.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There was some code like this: // Convenience to make things more legible in
I'm testing the following code: #include <iostream> #include <vector> #include <algorithm> #include <ctime> int
There are numerous libraries providing Linq capabilities to C# code interacting with a MySql
I have a table where I am dumping some transaction data. I have a
I just added some computationally expensive code to an Android game I am developing.
SQLServer int field. Value sometimes null. DataAdapter fills dataset OK and can display data
I've developed some concept code for a project that I will be working on
There are some Delegates predefined in C# I know these: EventHandler // Default event
There is a conversion process that is needed when migrating Visual Studio 2005 web
There are two weird operators in C#: the true operator the false operator If

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.