5

In MLState's new programming language Opa, is it possible to create a global variable?

J D
  • 48,105
  • 13
  • 171
  • 274

1 Answers1

5

Opa is a functional language so there are no global variables in the language. However, one can achieve a similar behavior with Mutable. At top-level one declares the value with:

global_var = Mutable.make(initial_value)

where initial_value is the initial value for the variable (of some type t). Then one can retrieve the value with:

global_var.get()

and set it with:

global_var.set(new_value)

More information in the Opa API overview.

Note however, that this mechanism should be used only in special situations; the primary device of encapsulating state in Opa are its distributed sessions (see the Opa manual for more on that subject).

akoprowski
  • 3,297
  • 1
  • 18
  • 26
  • Note that, for the usual value restriction issues, your global mutable variable cannot be polymorphic. – Yoric Jul 04 '11 at 07:48