Previous Contents Next
Chapter 7 Language extensions

This chapter describes the language features that are implemented in Objective Caml, but not described in the Objective Caml reference manual. In contrast with the fairly stable kernel language that is described in the reference manual, the extensions presented here are still experimental, and may be removed or changed in the future.

7.1 Streams and stream parsers

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 -pp camlp4o option to ocamlc and ocamlopt.

7.2 Range patterns

In patterns, Objective Caml recognizes the form ' c ' .. '  d ' (two character literals separated by ..) as shorthand for the pattern
' c ' | '  c1 ' | '  c2 ' | ... | '  cn ' | '  d '
where c1, c2, ..., cn are the characters that occur between c and d in the ASCII character set. For instance, the pattern '0'..'9' matches all characters that are digits.

7.3 Assertion checking

Objective Caml supports the assert construct to check debugging assertions. The expression assert expr evaluates the expression expr and returns () if expr evaluates to true. Otherwise, the exception Assert_failure is raised with the source file name and the location of expr as arguments. Assertion checking can be turned off with the -noassert compiler option.

As a special case, assert false is reduced to raise (Assert_failure ...), which is polymorphic (and is not turned off by the -noassert option).

7.4 Deferred computations

The expression lazy expr returns a value v of type Lazy.t that encapsulates the computation of expr. The argument expr is not evaluated at this point in the program. Instead, its evaluation will be performed the first time Lazy.force is applied to the value v, returning the actual value of expr. Subsequent applications of Lazy.force to v do not evaluate expr again.

The expression lazy expr is equivalent to ref(Lazy.Delayed(fun () -> expr)). For more information, see the description of module Lazy in the standard library ( Module Lazy ).

7.5 Local modules

The expression let module module-name =  module-expr in  expr locally binds the module expression module-expr to the identifier module-name during the evaluation of the expression expr. It then returns the value of expr. For example:
        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)

Previous Contents Next