OCAML the record field is not mutable

553 views Asked by At

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 ?

2

There are 2 answers

0
Anton Trunov On

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 }
1
Pierre G. On

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};;