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 7870697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:41:33+00:00 2026-06-03T01:41:33+00:00

I often need to make a core function that’s used in many places somehow

  • 0

I often need to make a core function that’s used in many places somehow configurable – i.e., it may use either algorithm A or algorithm B depending on a command-line switch; or have it print extra-verbose information to stdout if a ‘debug’ flag is set somehow.

How should I implement such global flags?

I see 4 options, all of them aren’t really good.

  1. Read command-line arguments from the function – bad, because that needs IO monad and the core calculation functions all are pure, I don’t want to get IO in there;

  2. Pass a parameter from main/IO all the way through to the ‘leaf’ function which needs to change behavior – completely unusable, as that means changing a dozen of unrelated functions in different modules to pass this parameter, and I want to try out such configuration options multiple times without changing the wrapping code every time;

  3. Use unsafePerformIO to get a true global variable – feels ugly and overkill for such a simple issue;

  4. Right in the middle of the function have code for both options and comment one of them out. Or have functions do_stuff_A and do_stuff_B, and change which one of them is called depending on what a global function needDebugInfo=True says. That’s what I’m doing now for the debuginfo, but it can’t be changed w/o recompile, and it shouldn’t really be the best available way…

I don’t need or want global mutable state – I want to have a simple global flag that’s immutable at runtime but can be somehow set when the program is launched. Are there any options ?

  • 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-06-03T01:41:37+00:00Added an answer on June 3, 2026 at 1:41 am

    Our new HFlags library is exactly for this.

    If you would like to see an example usage like your example, look into this:

    https://github.com/errge/hflags/blob/master/examples/ImportExample.hs

    https://github.com/errge/hflags/blob/master/examples/X/B.hs

    https://github.com/errge/hflags/blob/master/examples/X/Y_Y/A.hs

    No kind of parameter passing is needed between the modules, and you can define new flags with an easy syntax. It uses unsafePerformIO internally, but we think that it does that in a safe way, and you won’t have to concern yourself with that.

    There is a blog post about this stuff at: http://blog.risko.hu/2012/04/ann-hflags-0.html

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

Sidebar

Related Questions

I often need to use a function which performs and action X is condition
When programming for the iPhone, I find that I often need to use the
While debugging jQuery apps that use AJAX, I often have the need to see
I need to make use of a config file as often one does. In
You often need View in View. For example, a client that has many phone
I need to make application that needs to poll server often, but GAE has
I use Visual Studio 2010 and ReSharper 6.1. I often need to create some
I often make small websites and use the built in ASP.NET membership functionality in
I have a program that use schannel.dll to make a SSL connection to remote
Very often I need something like that: foreach (Line line in lines) { 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.