0x4d
0x4d

Reputation: 54

OCAML the record field is not mutable

I have this code:

type seed = {c: Graphics.color option; x : int; y : int };;
type voronoi = {dim : int * int; seeds : seed array};;

let v1 = {dim = 50,50; seeds = [| {c=Some Graphics.red; x=50; y=100}; |]}

when i'm trying this:

v1.seeds.(0).c <- Some Graphics.green;;

i get this error:

The record field c is not mutable

what can i do ?

Upvotes: 1

Views: 583

Answers (2)

Pierre G.
Pierre G.

Reputation: 4431

indeed, c is not mutable.

You should write like this :

v1.seeds.(0) <- {c=Some Graphics.green;x=v1.seeds.(0).x;y=v1.seeds.(0).y};;

Upvotes: 1

Anton Trunov
Anton Trunov

Reputation: 15404

Record fields are immutable, unless declared otherwise. The OCaml Reference Manual (sect 1.5) says that

Record fields can also be modified by assignment, provided they are declared mutable in the definition of the record type

The following should work:

type seed = {mutable c: Graphics.color option; x : int; y : int }

Upvotes: 3

Related Questions