I need to create a collection in F# that has a key value pair and is global in scope.
Variables declared Globally (outside any function) have Global Scope. Global variables can be accessed from anywhere in a JavaScript program. Variables declared with var , let and const are quite similar when declared outside a block. They all have Global Scope: var x = 2; // Global scope.
Global variables hold their values throughout the lifetime of your program and they can be accessed inside any of the functions defined for the program. A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration.
In a programming environment, the global scope is the scope that contains, and is visible in, all other scopes.
In simple terms, scope of a variable is its lifetime in the program. This means that the scope of a variable is the block of code in the entire program where the variable is declared, used, and can be modified.
You can do this:
[<AutoOpen>]
module Globals =
let map = System.Collections.Generic.Dictionary<_,_>()
Then use it unqualified throughout your program:
map.Add(1, "a")
map.Add(2, "b")
map |> Seq.iter (fun (KeyValue(k, v)) -> printfn "Key: %d, Value: %s" k v)
depending on what kind of project you are doing the best method might be do just declare it in a module:
module GlobalVals =
let myCollection = .... // whatever
you can just use it with
GlobalVals.myCollection...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With