ca9163d9
ca9163d9

Reputation: 29159

Properties of a module for these never changed (or just once at beginning) settings?

I have the following module, basically, the parameter yyyyMMdd, options, and folder are passed around for a lot of functions to be used. It will only be assigned/changed in the main once, or never need to be updated (folder, options).

How to implement "Property" of an F# module so there is no need to let all these functions have these parameters?

module X

let a theDate x options = .... // aDate is used here

let b theDate x folder options = a theDate 30 options //....

let c theDate folder options = b theDate 100 folder options //... other statements omitted. 

let main yyyyMMdd folder options =
    let aDate = .... // convert yyyyMMdd to type DateTime
    ...
    c aDate folder
    ...

Update: I defined the following type with static memebers for it. Is it F# idiomatic?

type Settings() =
    static member val TheDate= DateTime.MaxValue with get, set
    static member val Folder = "" with get, set
    static memeber val Options = ??? with get, set

Upvotes: 2

Views: 41

Answers (1)

Jwosty
Jwosty

Reputation: 3644

Mutable let bindings can do what you ask for:

module MyModule =
    let mutable myMutableValue = 42

    let f1 a b = ... // you can use myMutableValue inside

Then, to mutate it, do MyModule.myMutableValue <- xyz.

Note though that having a global mutable variable in a module is not idiomatic F#. Without knowing more about what you're trying to accomplish, though, it's tough to give better advice, but if I were to give it anyway, I'd say you should keep it as a parameter to all these functions (you'll thank yourself later when you don't have race conditions and other mutability related bugs).

Upvotes: 1

Related Questions