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

13876Re: "ocaml_beginners"::[] accessing record types

Expand Messages
  • Gabriel Scherer
    Apr 15, 2013
    • 0 Attachment

      It is rather difficult to know what you are asking for. I don't think
      there is any link between the (expr : type) syntax and any of what you
      describe in your email (in fact this syntax is mostly useless and
      extremely rarely used). I also have no idea what you mean by "nullary
      constructor" in your example. In my book a nullary constructor might
      be (I don't think I've seen the name used consistently) a variant
      constructor with no parameter

      type t =
      | Foo of int
      | Bar (* nullary ? *)

      which you have not used in your example.

      The language manual is intended as a language reference more than a
      comprehensive introduction for beginners. The website page
      http://ocaml.org/books.html has some books available online. In my
      experience both Jason Hickey's "Introduction to OCaml" and the online
      book "Developping Applications with OCaml" (available both in french
      or english) are good, comprehensive resources.

      On Mon, Apr 15, 2013 at 7:43 PM, Jean Saint-Remy
      <jeansaintremy@...> wrote:
      > Hi,
      > I am working on a no-frills database application, and wanted to better understand the nullary constructors.
      > There are some examples in the literature, but the details are scarce. Most of the time, we don't access types
      > directly. They are declared as memory containers so that data can be "safely" allocated, and we use data structures
      > throughout.
      > type db =
      > | Person of entry
      > | Weekly of sched
      > and entry =
      > {
      > first_name : string;
      > last_name : string;
      > }
      > and sched =
      > {
      > date : float;
      > starting : float;
      > ending : float;
      > } ;;
      > let molly : db = Person { first_name = "Molly"; last_name = "Peterson"; }
      > and week1 : db = Weekly { date = 1355062480.; starting = 1355103310.; ending = 1355122250.; } ;;
      > The molly and week1 statements can also be written separately. That would be currying.
      > There is also an idiomatic short-hand form
      > let molly = Person { first_name = "Molly"; last_name = "Peterson"; } ;;
      > The type engine understands that there are two fields of type string.
      > It seems we are creating an enumerated type, and these are declared just like records with (expr : type) syntax.To access the fields, can we use molly.first_name or molly.last_name besides writing a function "match x with"
      > to decompose the fields? I have read many programs where these types are simply plugged into data structures, as
      > long as the types are matching, everything works as expected. Obviously mixed type of string and float would become
      > a tuple or a hash and won't work with lists or arrays which expect one kind of an object. It seems there is a leap of
      > faith, the types are declared and then we just work with data structures with types being mere place holders.
      > Declaring a "db" type with 5 fields is very simple and works well. If we want String.length (molly.first_name) we
      > need to wrap the field in a data structure and access the elements. Is that all there is to the constructors? It took me
      > a while to figure out how to get from the type declaration to the instance of molly, until I hit upon the (expr : type)
      > with parenthesis required in the literature. OCaml core reference goes right away into pattern matching, so I could
      > not apply the constructor. Would you be so kind and explain the nullary constructor in a greater detail?
      > With kind regards,
      > Jean
      > [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
    • Show all 6 messages in this topic