Question

I have a question: What is it mutable field ? Is it connected with reference ? If I can create mutable "without" creating record Thanks in advcane!

Was it helpful?

Solution

Actually, references are implemented in terms of mutable fields: a reference is just a record with a single (mutable) field named contents, as you can see in your toplevel:

# let x = ref 1;;
val x : int ref = {contents = 1}
# x.contents;;
- : int = 1
# x.contents <- 2;;
- : unit = ()
# !x;;
- : int = 2

The main way to get mutable fields is to use records, and it is the way you should prefer. Objects have mutable instance variables. You can also get the equivalent of references by using one-element arrays.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top