Skip to content

Instantly share code, notes, and snippets.

@zbroyar
Created January 22, 2016 21:47
Show Gist options
  • Save zbroyar/71d74aeef6f23a303b57 to your computer and use it in GitHub Desktop.
Save zbroyar/71d74aeef6f23a303b57 to your computer and use it in GitHub Desktop.
Sum type example
(*ocamlfind ocamlopt -o sum_type -package xml-light sum_type.ml -linkpkg*)
open Printf
module X = Xml
type amazon_response_content = Xml of Xml.xml | Raw of string
let dump_response_content =
function
| Xml xml ->
begin
match xml with
| X.PCData s -> printf "%s\n%!" s
| X.Element (n, _, _) -> printf "%s\n%!" n
end
| Raw s -> printf "%s\n%!" s
let e () = Xml (X.PCData "XmlPCData")
let f () = Xml (X.Element ("XmlElement", [], []))
let g () = Raw "StringTest"
let h () = Xml (X.Element ("XmlElement2", [], []))
let l = [f;g;h;f;g;h;e]
let rec process =
function
| hd :: tl ->
let v = hd () in
dump_response_content v;
process tl
| [] -> ()
let _ = process l
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment