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

What does "option" mean ?

Expand Messages
  • yyu08
    Hi, all There is code as follows: type myType = .... let myVar : myType option = None ... It seems that both option and None are not keyword. Where in
    Message 1 of 4 , Mar 5, 2005
      Hi, all

      There is code as follows:

      type myType = ....

      let myVar : myType option = None

      ...

      It seems that both 'option' and 'None' are not keyword.
      Where in Ocaml are they defined?

      Thanks a lot!

      Andy
    • William D.Neumann
      ... a option is a built in datatype (like a list, or a array). I m not really sure where it s defined though, perhaps in pervasives.ml? William D. Neumann
      Message 2 of 4 , Mar 5, 2005
        On Mar 5, 2005, at 7:54 PM, yyu08 wrote:

        > It seems that both 'option' and 'None' are not keyword.
        > Where in Ocaml are they defined?

        'a option is a built in datatype (like 'a list, or 'a array). I'm not
        really sure where it's defined though, perhaps in pervasives.ml?

        William D. Neumann

        "You've got Rita Marlowe in the palm of your hand."
        "Palm of my hand? You haven't seen Rita Marlowe..."

        -- Will Success Spoil Rock Hunter?
      • Martin Jambon
        ... It is simply predefined, it doesn t belong to any referenceable module: $ ocaml Objective Caml version 3.08.2 # Pervasives.None;; Unbound constructor
        Message 3 of 4 , Mar 6, 2005
          On Sat, 5 Mar 2005, William D.Neumann wrote:

          >
          > On Mar 5, 2005, at 7:54 PM, yyu08 wrote:
          >
          > > It seems that both 'option' and 'None' are not keyword.
          > > Where in Ocaml are they defined?
          >
          > 'a option is a built in datatype (like 'a list, or 'a array). I'm not
          > really sure where it's defined though, perhaps in pervasives.ml?

          It is simply predefined, it doesn't belong to any referenceable module:

          $ ocaml
          Objective Caml version 3.08.2

          # Pervasives.None;;
          Unbound constructor Pervasives.None
          # Pervasives.FP_normal;;
          - : fpclass = FP_normal


          # Martin;;
          Unbound constructor Martin
        • Richard Jones
          ... I think they used to be defined in pervasives.mli (the Pervasives module), but it appears that in recent versions of OCaml they re actually built in to the
          Message 4 of 4 , Mar 6, 2005
            On Sun, Mar 06, 2005 at 02:54:25AM -0000, yyu08 wrote:
            > There is code as follows:
            >
            > type myType = ....
            > let myVar : myType option = None
            >
            > It seems that both 'option' and 'None' are not keyword.
            > Where in Ocaml are they defined?

            I think they used to be defined in pervasives.mli (the Pervasives
            module), but it appears that in recent versions of OCaml they're
            actually built in to the compiler.

            Anyhow, option is, or would be, defined by the following:

            type 'a option = None | Some of 'a

            If you're familiar with the C programming language, then what you've
            got here is essentially a NULL pointer.

            The OCaml string type, for example, represents strings. But what
            if you wanted to represent the "null string". Not "", which is the
            empty string, but the complete absence of data.

            Or to give another example. The int type represents numbers. Suppose
            you are conducting a survey of people's ages. If people refuse to
            tell you their age, how would you represent that? Representing it as
            0 or -1 isn't a good idea, because when you try to calculate the
            average age of your group, you'll get the wrong answer.

            In both these cases, the null / absence of data can be represented
            using an option type:

            string option

            None is the null string
            Some "" is the empty string
            Some "str" is a string

            type age = int option

            None is the case where you don't know the age
            Some 0 is for a newborn baby
            Some 40 is for a middle aged person

            The following won't compile, because you've forgotten about the "no
            data" case:

            let average_age ages = List.fold_left (+) 0 ages / List.length ages

            It needs to be fixed to take that into account:

            let average_age ages =
            let ages = ExtList.List.filter_map (fun age -> age) ages in
            List.fold_left (+) 0 ages / List.length ages

            Rich.

            --
            Richard Jones, CTO Merjis Ltd.
            Merjis - web marketing and technology - http://merjis.com
            Team Notepad - intranets and extranets for business - http://team-notepad.com
          Your message has been successfully submitted and would be delivered to recipients shortly.