Note that there are some explanatory texts on larger screens.

plurals
  1. POProper way to treat global flags in Haskell
    primarykey
    data
    text
    <p>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.</p> <p>How should I implement such global flags?</p> <p>I see 4 options, all of them aren't really good.</p> <p>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;</p> <p>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;</p> <p>3) Use unsafePerformIO to get a true global variable - feels ugly and overkill for such a simple issue;</p> <p>4) Right in the middle of the funcion 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...</p> <p>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 ?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload