Array bindings in OCaml?

Is there a way in OCaml to assign a binding, for example, to an array?
Something like:

array = [print_string "Hello" &where\n"

array.where = world

print_string = array

would result in:

Hello world

Hi @halcek! :wave:

Could you tell us a bit more about why you’d want to “assign a binding” to an array? Without knowing the context it is difficult to answer this.

There’s a simple idiomatic way to achieve something similar:

let array ~where =
  [|"Hello " ^ where; "Goodbye " ^ where|]

let () =
  Array.iter print_endline (array ~where:"world")

Which prints:

Hello world
Goodbye world

Note that this is using a function with a labeled argument ~where and let () = ... is a top-level statement, i.e., it will be run when the program starts.

Let us know if this helps.

1 Like