Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Style guidelines for global variables in F#

For a project I am working on I need a global variable(technically I don't, I could build it and then pass it to every single function call, and let every single function call know about it, but that seems just as hacky, less readable and more work.)

The global variables are look up tables(endgame, opening book and transpositions/cache) for a game.

The fact that some of the code may lose some of it's indempotent behavior is actually the point(speedups) in short, yes I know global mutable state is bad, it's really worth it in this case(10x+ performance improvement)

So here's the question, "build a singleton or use a static value in a static class with combinators"

They are effectively identical but I am curious what people have done before on this sort of problem

Or alternatively, should I be passing the thing around to everyone(or at least a reference to it anyways),is that really the best answer?

like image 485
Snark Avatar asked May 18 '11 06:05

Snark


People also ask

Can you use global variables in a function?

Global variables can be used by everyone, both inside of functions and outside.

What is global F in Python?

In Python, global keyword allows you to modify the variable outside of the current scope. It is used to create a global variable and make changes to the variable in a local context.

How do you assign a value to a global variable in a function?

There are two ways to declare a variable globally: Declare a variable outside the functions. Assign value to a variable inside a function without declaring it using “var” keyword.

Should global variables be capitalized?

Naming Convention for Global Variables It is recommended to use camel case for mutable global variables and uppercase for immutable global variables.


2 Answers

Here is a solution similar to the one posted by @Yin Zhu's, but using abstract types to specify a usage interface for the mutable value, a local definition to encapsulate it and object literals to provide an implementation (this is taken from Expert F#--which is co-authored by Don Syme):

type IPeekPoke =
    abstract member Peek: unit -> int
    abstract member Poke: int -> unit

let makeCounter initialState =
    let state = ref initialState
    { new IPeekPoke with
        member x.Poke(n) = state := !state + n
        member x.Peek() = !state }
like image 189
Alex Avatar answered Nov 06 '22 22:11

Alex


You can also do it with static fields, like this:

type Common() = 

    static let mutable queue : CloudQueue = null
    static let mutable storageAccount : CloudStorageAccount = null

    static member Queue 
        with get() = queue
        and set v = queue <- v
    static member StorageAccount 
        with get() = storageAccount
        and set v = storageAccount <- v

In another module, just:

open Common
Common.Queue <- xxxx
like image 40
Bill Donahue Avatar answered Nov 06 '22 21:11

Bill Donahue