Loading ...
Sorry, an error occurred while loading the content.

Re: "ocaml_beginners"::[] Questions about reference

Expand Messages
  • Chingfan Tsou
    ... Did you mean the compiler is smart enough to often avoid really making a copy ?
    Message 1 of 8 , Jun 19, 2013
    View Source
    • 0 Attachment
      > It never saves a copy, it is always a relation. If the record field is
      > mutable then the "relation" can change, otherwise the "relation" is fixed.

      Did you mean "the compiler is smart enough to often avoid
      really making a copy"?
    • Francois Berenger
      ... OCaml refs are more like C++ references in fact, because we never have null pointers in OCaml. And, that removes a hell of a lot of possible errors. That s
      Message 2 of 8 , Jun 19, 2013
      View Source
      • 0 Attachment
        On 06/20/2013 03:37 AM, Ashish Agarwal wrote:
        > Yes, that's conceptually correct. If you know languages like C, you can
        > think of ref as a pointer.

        OCaml refs are more like C++ references in fact, because we never have
        null pointers in OCaml.
        And, that removes a hell of a lot of possible errors.
        That's a shame Go did not get this right.

        > When you say "let x = ref u", you define x to be
        > a pointer to u. Furthermore, you defined u to be a pointer to a memory
        > location holding the value 1. Finally, you changed what was in this memory
        > location to 2.
        >
        > Note that for immutable values, it is correct to logically think you are
        > always getting a copy, but the compiler is smart enough to often avoid
        > really making a copy. In other words, don't use ref's unnecessarily.
        > Program with immutable values by default, and use ref's or other mutable
        > data structures only if really needed.
        >
        > On Wed, Jun 19, 2013 at 2:13 PM, Chingfan Tsou <caojingfan@...> wrote:
        >
        >> **
        >>
        >>
        >> Ah, does that mean if the contents is immutable value, it'll save a copy of
        >> the value; otherwise, if it's a ref of something(A for example), it won't
        >> save a copy but instead, just save a "relation" so it'll be changed if A is
        >> changed. Am I right?
        >>
        >>
        >> [Non-text portions of this message have been removed]
        >>
        >>
        >>
        >
        >
        > [Non-text portions of this message have been removed]
        >
        >
        >
        > ------------------------------------
        >
        > Archives up to December 31, 2011 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners
        > The archives of the very official ocaml list (the seniors' one) can be found at http://caml.inria.fr
        > Attachments are banned and you're asked to be polite, avoid flames etc.Yahoo! Groups Links
        >
        >
        >
      Your message has been successfully submitted and would be delivered to recipients shortly.