Import Upstream version 20180207
[hcoop/debian/mlton.git] / doc / examples / size / size.sml
1 fun 'a printSize (name: string, value: 'a): unit=
2 (print "The size of "
3 ; print name
4 ; print " is "
5 ; print (Int.toString (MLton.size value))
6 ; print " bytes.\n")
7
8 val l = [1, 2, 3, 4]
9
10 val _ =
11 (
12 printSize ("an int list of length 4", l)
13 ; printSize ("a string of length 10", "0123456789")
14 ; printSize ("an int array of length 10", Array.tabulate (10, fn _ => 0))
15 ; printSize ("a double array of length 10",
16 Array.tabulate (10, fn _ => 0.0))
17 ; printSize ("an array of length 10 of 2-ples of ints",
18 Array.tabulate (10, fn i => (i, i + 1)))
19 ; printSize ("a useless function", fn _ => 13)
20 )
21
22 (* This is here so that the list is "useful".
23 * If it were removed, then the optimizer (remove-unused-constructors)
24 * would remove l entirely.
25 *)
26 val _ = if 10 = foldl (op +) 0 l
27 then ()
28 else raise Fail "bug"
29
30 local
31 open MLton.Cont
32 in
33 val rc: int option t option ref = ref NONE
34 val _ =
35 case callcc (fn k: int option t => (rc := SOME k; throw (k, NONE))) of
36 NONE => ()
37 | SOME i => print (concat [Int.toString i, "\n"])
38 end
39
40 val _ = printSize ("a continuation option ref", rc)
41
42 val _ =
43 case !rc of
44 NONE => ()
45 | SOME k => (rc := NONE; MLton.Cont.throw (k, SOME 13))