문제

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