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!

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top