Skip to content

Instantly share code, notes, and snippets.

@dmitry-a-morozov
Created April 4, 2016 23:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmitry-a-morozov/346c1eecfa37a87c88e03872023a5a41 to your computer and use it in GitHub Desktop.
Save dmitry-a-morozov/346c1eecfa37a87c88e03872023a5a41 to your computer and use it in GitHub Desktop.
thread safe singleton defined as F# record
type Configuration = {
Database: string
RetryCount: int
}
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
[<AutoOpen>]
module Configuration =
let private singleton = ref { Database = "(local)"; RetryCount = 3 }
let private guard = obj()
type Configuration with
static member Current
with get() = lock guard <| fun() -> !singleton
and set value = lock guard <| fun() -> singleton := value
printfn "Default start-up config: %A" Configuration.Current
Configuration.Current <- { Configuration.Current with Database = ".\SQLExpress" }
printfn "Updated config: %A" Configuration.Current
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment