Guy Coder
Guy Coder

Reputation: 24996

How is global scoping done in F#

I need to create a collection in F# that has a key value pair and is global in scope.

Upvotes: 5

Views: 1245

Answers (2)

Daniel
Daniel

Reputation: 47914

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)

Upvotes: 7

Random Dev
Random Dev

Reputation: 52300

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...

Upvotes: 4

Related Questions