<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <HTML> <HEAD> <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <META name="GENERATOR" content="hevea 1.06-7 of 2001-11-14"> <TITLE> Language extensions </TITLE> </HEAD> <BODY TEXT=black BGCOLOR=white> <A HREF="manual008.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A> <A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A> <A HREF="manual022.html"><IMG SRC ="next_motif.gif" ALT="Next"></A> <HR> <TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#2de52d"><DIV ALIGN=center><TABLE> <TR><TD><A NAME="htoc90"><B><FONT SIZE=6>Chapter 7</FONT></B></A></TD> <TD WIDTH="100%" ALIGN=center><B><FONT SIZE=6>Language extensions</FONT></B></TD> </TR></TABLE></DIV></TD> </TR></TABLE> <A NAME="c:extensions"></A> <BR> This chapter describes syntactic extensions and convenience features that are implemented in Objective Caml, but not described in the Objective Caml reference manual.<BR> <BR> <TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#66ff66"><DIV ALIGN=center><TABLE> <TR><TD><A NAME="htoc91"><B><FONT SIZE=5>7.1</FONT></B></A></TD> <TD WIDTH="100%" ALIGN=center><B><FONT SIZE=5>Streams and stream parsers</FONT></B></TD> </TR></TABLE></DIV></TD> </TR></TABLE> <A NAME="s:streams"></A><BR> Streams and stream parsers are no longer part of the Objective Caml language, but available through a CamlP4 syntax extension. See the CamlP4 reference manual for more information. Objective Caml programs that use streams and stream parsers can be compiled with the <TT>-pp camlp4o</TT> option to <TT>ocamlc</TT> and <TT>ocamlopt</TT>. For interactive use, run <TT>ocaml</TT> and issue the <CODE>#load "camlp4o.cma";;</CODE> command.<BR> <BR> <TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#66ff66"><DIV ALIGN=center><TABLE> <TR><TD><A NAME="htoc92"><B><FONT SIZE=5>7.2</FONT></B></A></TD> <TD WIDTH="100%" ALIGN=center><B><FONT SIZE=5>Range patterns</FONT></B></TD> </TR></TABLE></DIV></TD> </TR></TABLE><BR> In patterns, Objective Caml recognizes the form <TT><FONT COLOR=blue>'</FONT></TT> <TT><I><FONT COLOR=maroon>c</FONT></I></TT> <TT><FONT COLOR=blue>'</FONT></TT> <TT><FONT COLOR=blue>..</FONT></TT> <TT><FONT COLOR=blue>'</FONT></TT> <TT><I><FONT COLOR=maroon>d</FONT></I></TT> <TT><FONT COLOR=blue>'</FONT></TT> (two character literals separated by <TT>..</TT>) as shorthand for the pattern <DIV ALIGN=center> <TT><FONT COLOR=blue>'</FONT></TT> <TT><I><FONT COLOR=maroon>c</FONT></I></TT> <TT><FONT COLOR=blue>'</FONT></TT> <TT><FONT COLOR=blue>|</FONT></TT> <TT><FONT COLOR=blue>'</FONT></TT> <TT><I><FONT COLOR=maroon>c</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> <TT><FONT COLOR=blue>'</FONT></TT> <TT><FONT COLOR=blue>|</FONT></TT> <TT><FONT COLOR=blue>'</FONT></TT> <TT><I><FONT COLOR=maroon>c</FONT></I></TT><SUB><FONT SIZE=2>2</FONT></SUB> <TT><FONT COLOR=blue>'</FONT></TT> <TT><FONT COLOR=blue>|</FONT></TT> ... <TT><FONT COLOR=blue>|</FONT></TT> <TT><FONT COLOR=blue>'</FONT></TT> <TT><I><FONT COLOR=maroon>c</FONT></I></TT><SUB><FONT SIZE=2><I>n</I></FONT></SUB> <TT><FONT COLOR=blue>'</FONT></TT> <TT><FONT COLOR=blue>|</FONT></TT> <TT><FONT COLOR=blue>'</FONT></TT> <TT><I><FONT COLOR=maroon>d</FONT></I></TT> <TT><FONT COLOR=blue>'</FONT></TT> </DIV> where <I>c</I><SUB><FONT SIZE=2>1</FONT></SUB>, <I>c</I><SUB><FONT SIZE=2>2</FONT></SUB>, ..., <I>c</I><SUB><FONT SIZE=2><I>n</I></FONT></SUB> are the characters that occur between <I>c</I> and <I>d</I> in the ASCII character set. For instance, the pattern <TT>'0'..'9'</TT> matches all characters that are digits.<BR> <BR> <TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#66ff66"><DIV ALIGN=center><TABLE> <TR><TD><A NAME="htoc93"><B><FONT SIZE=5>7.3</FONT></B></A></TD> <TD WIDTH="100%" ALIGN=center><B><FONT SIZE=5>Assertion checking</FONT></B></TD> </TR></TABLE></DIV></TD> </TR></TABLE> <A NAME="@manual.kwd168"></A><BR> Objective Caml supports the <TT>assert</TT> construct to check debugging assertions. The expression <TT><FONT COLOR=blue>assert</FONT></TT> <TT><I><FONT COLOR=maroon>expr</FONT></I></TT> evaluates the expression <TT><I><FONT COLOR=maroon>expr</FONT></I></TT> and returns <TT>()</TT> if <TT><I><FONT COLOR=maroon>expr</FONT></I></TT> evaluates to <TT>true</TT>. Otherwise, the exception <TT>Assert_failure</TT> is raised with the source file name and the location of <TT><I><FONT COLOR=maroon>expr</FONT></I></TT> as arguments. Assertion checking can be turned off with the <TT>-noassert</TT> compiler option.<BR> <BR> As a special case, <TT>assert false</TT> is reduced to <TT>raise (Assert_failure ...)</TT>, which is polymorphic (and is not turned off by the <TT>-noassert</TT> option). <A NAME="@manual3"></A><BR> <BR> <TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#66ff66"><DIV ALIGN=center><TABLE> <TR><TD><A NAME="htoc94"><B><FONT SIZE=5>7.4</FONT></B></A></TD> <TD WIDTH="100%" ALIGN=center><B><FONT SIZE=5>Deferred computations</FONT></B></TD> </TR></TABLE></DIV></TD> </TR></TABLE> <A NAME="@manual.kwd169"></A><BR> The expression <TT><FONT COLOR=blue>lazy</FONT></TT> <TT><I><FONT COLOR=maroon>expr</FONT></I></TT> returns a value <I>v</I> of type <TT>Lazy.t</TT> that encapsulates the computation of <TT><I><FONT COLOR=maroon>expr</FONT></I></TT>. The argument <TT><I><FONT COLOR=maroon>expr</FONT></I></TT> is not evaluated at this point in the program. Instead, its evaluation will be performed the first time <TT>Lazy.force</TT> is applied to the value <I>v</I>, returning the actual value of <TT><I><FONT COLOR=maroon>expr</FONT></I></TT>. Subsequent applications of <TT>Lazy.force</TT> to <I>v</I> do not evaluate <TT><I><FONT COLOR=maroon>expr</FONT></I></TT> again. For more information, see the description of module <TT>Lazy</TT> in the standard library (<a HREF=libref/Lazy.html>Module <tt>Lazy</tt></a> ). <A NAME="@manual4"></A><A NAME="@manual5"></A><BR> <BR> <TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#66ff66"><DIV ALIGN=center><TABLE> <TR><TD><A NAME="htoc95"><B><FONT SIZE=5>7.5</FONT></B></A></TD> <TD WIDTH="100%" ALIGN=center><B><FONT SIZE=5>Local modules</FONT></B></TD> </TR></TABLE></DIV></TD> </TR></TABLE> <A NAME="@manual.kwd170"></A> <A NAME="@manual.kwd171"></A><BR> The expression <TT><FONT COLOR=blue>let</FONT></TT> <TT><FONT COLOR=blue>module</FONT></TT> <TT><I><FONT COLOR=maroon>module-name</FONT></I></TT> <TT><FONT COLOR=blue>=</FONT></TT> <TT><I><FONT COLOR=maroon>module-expr</FONT></I></TT> <TT><FONT COLOR=blue>in</FONT></TT> <TT><I><FONT COLOR=maroon>expr</FONT></I></TT> locally binds the module expression <TT><I><FONT COLOR=maroon>module-expr</FONT></I></TT> to the identifier <TT><I><FONT COLOR=maroon>module-name</FONT></I></TT> during the evaluation of the expression <TT><I><FONT COLOR=maroon>expr</FONT></I></TT>. It then returns the value of <TT><I><FONT COLOR=maroon>expr</FONT></I></TT>. For example: <PRE> let remove_duplicates comparison_fun string_list = let module StringSet = Set.Make(struct type t = string let compare = comparison_fun end) in StringSet.elements (List.fold_right StringSet.add string_list StringSet.empty) </PRE> <TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#66ff66"><DIV ALIGN=center><TABLE> <TR><TD><A NAME="htoc96"><B><FONT SIZE=5>7.6</FONT></B></A></TD> <TD WIDTH="100%" ALIGN=center><B><FONT SIZE=5>Grouping in integer and floating-point literals</FONT></B></TD> </TR></TABLE></DIV></TD> </TR></TABLE><BR> In integer and floating-point literals, the character <TT>_</TT> (underscore) can be used to separate groups of digits, as in <TT>1_000_000</TT>, <TT>0x45_FF</TT>, or <TT>1_234.567_89</TT>. The underscore characters are simply ignored when reading the literal. <BR> <BR> <HR> <A HREF="manual008.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A> <A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A> <A HREF="manual022.html"><IMG SRC ="next_motif.gif" ALT="Next"></A> </BODY> </HTML>