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

Re: "ocaml_beginners"::[] A question about "option"

Expand Messages
  • Martin Jambon
    ... Hi, a option is a predefined type, which is absolutely not magic and certainly not a keyword. You could have defined it by yourself, but this one is
    Message 1 of 7 , Aug 1 1:43 AM
    • 0 Attachment
      On Sat, 31 Jul 2004, Andy Yang wrote:

      > Hi, all
      >
      > Do you know where I can find more introduction on the
      > keyword "option" in Ocaml?
      >
      > Basically, option is defined as follows:
      > type 'a option = None | Some of 'a;
      >
      > And I found some codes as follows:
      >
      > class backup
      > object ('a)
      > val mutable copy : 'a option
      > method ...
      > end;;
      >
      > If it means a weak pointer, what will happen if the
      > garbage collector reclaims the memory?

      Hi,

      'a option is a predefined type, which is absolutely not magic and
      certainly not a keyword.
      You could have defined it by yourself, but this one is predefined in the
      core library of OCaml.
      Normally, this type is used to distinguish between something that has been
      defined or specified (Some x) and something which is undefined (None).

      In C, you could use the NULL pointer instead of a valid pointer to express
      this, but in OCaml you will never have the chance to manipulate invalid
      data such as an invalid pointer or an uninitialized array.

      Have a look at the chapter 3 of Richard Jones' tutorial:
      http://www.merjis.com/developers/ocaml_tutorial/


      Martin
    • Andy Yang
      I found there are codes as follows: let outChannel : out_channel option ref = ref None let mergedChannel : out_channel option ref = ref None Here, option is
      Message 2 of 7 , Aug 1 9:58 AM
      • 0 Attachment
        I found there are codes as follows:

        let outChannel : out_channel option ref = ref None
        let mergedChannel : out_channel option ref = ref None


        Here, "option" is not used in the 'a option form. Does
        it mean tha outChannel and mergedChannel are two weak
        pointer to references, and these two referenced can be
        reclaimed by the garbage collector at any time?

        Thanks!

        Andy

        --- Martin Jambon <martin_jambon@...> wrote:

        > On Sat, 31 Jul 2004, Andy Yang wrote:
        >
        > > Hi, all
        > >
        > > Do you know where I can find more introduction on
        > the
        > > keyword "option" in Ocaml?
        > >
        > > Basically, option is defined as follows:
        > > type 'a option = None | Some of 'a;
        > >
        > > And I found some codes as follows:
        > >
        > > class backup
        > > object ('a)
        > > val mutable copy : 'a option
        > > method ...
        > > end;;
        > >
        > > If it means a weak pointer, what will happen if
        > the
        > > garbage collector reclaims the memory?
        >
        > Hi,
        >
        > 'a option is a predefined type, which is absolutely
        > not magic and
        > certainly not a keyword.
        > You could have defined it by yourself, but this one
        > is predefined in the
        > core library of OCaml.
        > Normally, this type is used to distinguish between
        > something that has been
        > defined or specified (Some x) and something which is
        > undefined (None).
        >
        > In C, you could use the NULL pointer instead of a
        > valid pointer to express
        > this, but in OCaml you will never have the chance to
        > manipulate invalid
        > data such as an invalid pointer or an uninitialized
        > array.
        >
        > Have a look at the chapter 3 of Richard Jones'
        > tutorial:
        > http://www.merjis.com/developers/ocaml_tutorial/
        >
        >
        > Martin
        >
        >


        __________________________________________________
        Do You Yahoo!?
        Tired of spam? Yahoo! Mail has the best spam protection around
        http://mail.yahoo.com
      • james woodyatt
        ... No. Arrays of weak pointers are found in the Weak module of the standard library. -- j h woodyatt markets are only free to the people
        Message 3 of 7 , Aug 1 10:11 AM
        • 0 Attachment
          On 01 Aug 2004, at 09:58, Andy Yang wrote:
          >
          > Here, "option" is not used in the 'a option form. Does
          > it mean tha outChannel and mergedChannel are two weak
          > pointer to references, and these two referenced can be
          > reclaimed by the garbage collector at any time?

          No. Arrays of weak pointers are found in the Weak module of the
          standard library.


          --
          j h woodyatt <jhw@...>
          markets are only free to the people who own them.
        • Benjamin Geer
          ... No. The option type has nothing to do with weak pointers. Ben
          Message 4 of 7 , Aug 1 10:33 AM
          • 0 Attachment
            Andy Yang wrote:
            > Here, "option" is not used in the 'a option form. Does
            > it mean tha outChannel and mergedChannel are two weak
            > pointer to references, and these two referenced can be
            > reclaimed by the garbage collector at any time?

            No. The option type has nothing to do with weak pointers.

            Ben
          • Matt Gushee
            ... No, not at all. Having out_channel option rather than a option just means that the type is completely specified. If you were to specify a option in a
            Message 5 of 7 , Aug 1 10:35 AM
            • 0 Attachment
              On Sun, Aug 01, 2004 at 09:58:51AM -0700, Andy Yang wrote:
              >
              > I found there are codes as follows:
              >
              > let outChannel : out_channel option ref = ref None
              > let mergedChannel : out_channel option ref = ref None
              >
              >
              > Here, "option" is not used in the 'a option form. Does
              > it mean tha outChannel and mergedChannel are two weak
              > pointer to references, and these two referenced can be
              > reclaimed by the garbage collector at any time?

              No, not at all.

              Having 'out_channel option' rather than ''a option' just means that the
              type is completely specified. If you were to specify 'a option in a type
              or function signature, it would indicate polymorphism. Since this is a
              non-functional value, it can't be polymorphic, but 'a option would allow
              the specific type represented by 'a to be defined later.

              Since it is a ref, it is mutable. So this example is equivalent to
              declaring (but not initializing) a variable in C.

              It seems you really want to make a connection between the option type
              and weak pointers. As far as I know there is none. Are you just trying
              to learn, or do you have a specific usage in mind?

              --
              Matt Gushee When a nation follows the Way,
              Englewood, Colorado, USA Horses bear manure through
              mgushee@... its fields;
              http://www.havenrock.com/ When a nation ignores the Way,
              Horses bear soldiers through
              its streets.

              --Lao Tzu (Peter Merel, trans.)
            • Richard Jones
              ... The types are read backwards. Example: int list is a list of integers int option list is a list of optional integers int list option is an optional
              Message 6 of 7 , Aug 1 10:45 AM
              • 0 Attachment
                On Sun, Aug 01, 2004 at 09:58:51AM -0700, Andy Yang wrote:
                >
                > I found there are codes as follows:
                >
                > let outChannel : out_channel option ref = ref None
                > let mergedChannel : out_channel option ref = ref None

                The types are read backwards. Example:

                int list is a list of integers
                int option list is a list of optional integers
                int list option is an optional list of integers
                int ref is a reference to an integer
                int ref list is a list of references to integers

                So out_channel option ref is a reference to an optional output
                channel.

                Rich.

                --
                Richard Jones. http://www.annexia.org/ http://www.j-london.com/
                Merjis Ltd. http://www.merjis.com/ - improving website return on investment
                NET::FTPSERVER is a full-featured, secure, configurable, database-backed
                FTP server written in Perl: http://www.annexia.org/freeware/netftpserver/
              Your message has been successfully submitted and would be delivered to recipients shortly.