Version française
Home     About     Download     Resources     Contact us    
Browse thread
OCaml/C variant representation
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Richard Jones <rich@a...>
Subject: Re: [Caml-list] OCaml/C variant representation
On Wed, Feb 24, 2010 at 10:26:22AM -0500, Jean Yang wrote:
> In the body of this function I am looking at the fields of val.  Am I doing
> something wrong here?

As a general tip, use the extlib function Std.dump (or look at the
source of Std.dump) to see how values are represented at runtime.

For example:

$ rlwrap ocaml
        Objective Caml version 3.11.2

# #use "topfind";;
- : unit = ()
Findlib has been successfully loaded. Additional directives:
  #require "package";;      to load a package
  #list;;                   to list the available packages
  #camlp4o;;                to load camlp4 (standard syntax)
  #camlp4r;;                to load camlp4 (revised syntax)
  #predicates "p,q,...";;   to set these predicates
  Topfind.reset();;         to force that packages will be reloaded
  #thread;;                 to enable threads
- : unit = ()

# #require "extlib";;
/usr/lib64/ocaml/extlib: added to search path
/usr/lib64/ocaml/extlib/extLib.cma: loaded

# type t = A | B | C of int | D of int ;;
type t = A | B | C of int | D of int
# Std.dump A ;;
- : string = "0"                    # Stored as the OCaml int 0
# Std.dump B ;;
- : string = "1"                    # Stored as the OCaml int 1
# Std.dump (C 42) ;;
- : string = "(42)"                 # One-element block, tag 0
# Std.dump (D 42) ;;
- : string = "Tag1 (42)"            # One-element block, tag 1

# Std.dump ;;
- : 'a -> string = <fun>
# Std.dump `Foo ;;
- : string = "3505894"              # OCaml int, the hash of string "Foo"
# Std.dump (`Foo 1) ;;
- : string = "(3505894, 1)"         # Two-element block, tag 0
# Std.dump (`Foo (1, 2)) ;;
- : string = "(3505894, (1, 2))"    # Two-elem block, second field is
                                    # another block.

# Std.dump None ;;
- : string = "0"
# Std.dump (Some "foo") ;;
- : string = "(\"foo\")"

Rich.

-- 
Richard Jones
Red Hat