sample.ml 4.11 KB
Newer Older
gerd's avatar
gerd committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
(* This is an example how to use the new syntactic elements: *)

(**********************************************************************)
(*                       interpolate                                  *)
(**********************************************************************)

(* interpolate "string":
 * In the string literal brace expansion is performed. The following
 * notations are allowed:
 * $name                expands to the value of the string variable 'name'
 * $Module.name         expands to the value of the string variable 'name'
 *                      of 'Module'
 * ${name}              same as $name
 * ${Module.name}       same as $Module.name
 * ${name,%format}      expands to the value of the variable 'name' which
 *                      has been converted to a string using "%format".
 *                      The format string may be anything allowed in printf.
 * ${Module.name,%format}    works,too
 * \$                   A dollar character
 * \<newline>           Expands to the empty string
 * All backslash sequences of normal string constants are allowed, too.
 *
 * NOTE: For non-string variables a format specification is required;
 * otherwise type checking is impossible.
 *)

let s = "The number" in
let f = 3.14 in
let i = 42 in
print_string interpolate "$s ${f,%f} is not ${i,%d}\n";;

(**********************************************************************)
(*                     interpolate file                               *)
(**********************************************************************)

(* interpolate file "filename":
 * expands to the contents of the file; brace expansion is performed
 * (see above).
 * If "filename" is written without "/", it is always searched in the
 * same directory as the source file being compiled. Otherwise "filename"
 * is interpreted as relative or absolute path name.
 *
 * IMPORTANT NOTE: Of course, the file is only read during compile time.
 *)

let s = "The number" in
let f = 3.14 in
let i = 42 in
print_string interpolate file "sample.file";;

gerd's avatar
gerd committed
51
(**********************************************************************)
gerd's avatar
gerd committed
52
(*                        include_file                                *)
gerd's avatar
gerd committed
53 54
(**********************************************************************)

gerd's avatar
gerd committed
55
(* include_file "filename":
gerd's avatar
gerd committed
56 57 58 59 60 61
 * expands to the contents of the file but _no_ brace expansion is performed.
 * If "filename" is written without "/", it is always searched in the
 * same directory as the source file being compiled. Otherwise "filename"
 * is interpreted as relative or absolute path name.
 *
 * IMPORTANT NOTE: Of course, the file is only read during compile time.
gerd's avatar
gerd committed
62 63 64 65
 *
 * Note: Up to xstrp4-1.4, this construction used the notation
 * "include file". In xstrp4-1.5, it was changed to "include_file"
 * to avoid ambiguities with O'Caml's built-in "include" directive.
gerd's avatar
gerd committed
66 67 68
 *)

print_string "sample.file: ";
gerd's avatar
gerd committed
69
print_string include_file "sample.file";;
gerd's avatar
gerd committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

(**********************************************************************)
(*                      <:here< quotations>>                          *)
(**********************************************************************)

(* It is also possible to use the 'here' quotation which does brace
 * expansion on its argument. This is sometimes easier to write because
 * the double quotes need no escaping. Of course, $ and \ characters
 * must still be escaped.
 * Only \$, \<newline>, \>, and \\ are allowed as backslash sequences.
 *)

let s = "The number" in
let f = 3.14 in
let i = 42 in
print_string <:here<\
The interpolation example was:
print_string interpolate "\$s \${f,%f} is not \${i,%d}\n";;
-- where s was replaced by "$s", f by "${f,%f}", and i by "${i,%d}".
>>


gerd's avatar
gerd committed
92 93 94 95 96 97 98
(**********************************************************************)
(*                      Access to record fields                       *)
(**********************************************************************)

(* It is also possible to access record fields. Just use the standard
 * OCaml notation rcrd.field.
 *)
gerd's avatar
gerd committed
99

gerd's avatar
gerd committed
100 101 102 103 104 105 106 107
type rcrd = 
  {
    s: string;
    f: float;
    i: int;
  }
let rcrd = {s = "The number"; f = 3.14; i = 42} in
print_string interpolate "$rcrd.s ${rcrd.f,%f} is not ${rcrd.i,%d}\n";;