|
| 1 | +The reference files for this example are in the |
| 2 | +[class\_type](../../../examples/docs/methods/code_constructs/class_type) directory. |
| 3 | + |
| 4 | +The reference takes place in `/tmp/docs/methods/code_constructs`, which |
| 5 | +is a copy of the [code\_constructs](../../../examples/docs/methods/code_constructs) |
| 6 | +directory. Reported locations may differ depending on the location of the source |
| 7 | +files. |
| 8 | + |
| 9 | +The compilation command is : |
| 10 | +``` |
| 11 | +make -C class_type build |
| 12 | +``` |
| 13 | + |
| 14 | +The analysis command is : |
| 15 | +``` |
| 16 | +make -C class_type analyze |
| 17 | +``` |
| 18 | + |
| 19 | +The compile + analyze command is : |
| 20 | +``` |
| 21 | +make -C class_type |
| 22 | +``` |
| 23 | + |
| 24 | +> [!IMPORTANT] |
| 25 | +> **LIMITATION** |
| 26 | +> |
| 27 | +> Methods declared in class types definition (different from class signatures) |
| 28 | +> are currently ignored by the analyzer. |
| 29 | +
|
| 30 | +## First run |
| 31 | + |
| 32 | +Code: |
| 33 | +```OCaml |
| 34 | +(* class_type_lib.mli *) |
| 35 | +class type counter = object |
| 36 | + method get : int |
| 37 | + method incr : unit |
| 38 | + method reset : unit |
| 39 | +end |
| 40 | +
|
| 41 | +class type int_stack = object |
| 42 | + method push : int -> unit |
| 43 | + method pop : unit |
| 44 | + method peek : int option |
| 45 | + method reset : unit |
| 46 | +end |
| 47 | +
|
| 48 | +val counter : counter |
| 49 | +
|
| 50 | +val int_stack : int_stack |
| 51 | +``` |
| 52 | +```OCaml |
| 53 | +(* class_type_lib.ml *) |
| 54 | +class type counter = object |
| 55 | + method get : int |
| 56 | + method incr : unit |
| 57 | + method reset : unit |
| 58 | +end |
| 59 | +
|
| 60 | +class type int_stack = object |
| 61 | + method push : int -> unit |
| 62 | + method pop : unit |
| 63 | + method peek : int option |
| 64 | + method reset : unit |
| 65 | +end |
| 66 | +
|
| 67 | +let counter = object |
| 68 | + val mutable n = 0 |
| 69 | + method get = n |
| 70 | + method incr = n <- n + 1 |
| 71 | + method reset = n <- 0 |
| 72 | +end |
| 73 | +
|
| 74 | +let int_stack = object |
| 75 | + val mutable l : int list = [] |
| 76 | +
|
| 77 | + method push x = l <- x::l |
| 78 | +
|
| 79 | + method pop = |
| 80 | + match l with |
| 81 | + | [] -> () |
| 82 | + | _::tl -> l <- tl |
| 83 | +
|
| 84 | + method peek = |
| 85 | + match l with |
| 86 | + | [] -> None |
| 87 | + | hd::_ -> Some hd |
| 88 | +
|
| 89 | + method reset = l <- [] |
| 90 | +end |
| 91 | +``` |
| 92 | +```OCaml |
| 93 | +(* class_type_bin.ml *) |
| 94 | +class type unused_class = object |
| 95 | + method unused_method : unit |
| 96 | +end |
| 97 | +
|
| 98 | +let push_n_times n counter stack = |
| 99 | + for i = 1 to n do |
| 100 | + stack#push i; |
| 101 | + counter#incr; |
| 102 | + done |
| 103 | +
|
| 104 | +let clear_stack counter stack = |
| 105 | + while stack#peek <> None do |
| 106 | + stack#pop; |
| 107 | + counter#incr; |
| 108 | + done |
| 109 | +
|
| 110 | +let () = |
| 111 | + let open Class_type_lib in |
| 112 | + let n = 42 in |
| 113 | + counter#reset; |
| 114 | + push_n_times n counter int_stack; |
| 115 | + let count = counter#get in |
| 116 | + assert (count = n); |
| 117 | + counter#reset; |
| 118 | + clear_stack counter int_stack; |
| 119 | + let count' = counter#get in |
| 120 | + assert (count' = n) |
| 121 | +``` |
| 122 | + |
| 123 | +By looking at the code, we could make the same observation as in the |
| 124 | +[Class](./CLASS.md) example. |
| 125 | + |
| 126 | +However, because of the current limitation on class types, nothing is expected |
| 127 | +to reported. |
| 128 | + |
| 129 | +Code and analyze: |
| 130 | +``` |
| 131 | +$ make -C class_type |
| 132 | +make: Entering directory '/tmp/docs/methods/code_construct/class_type' |
| 133 | +ocamlopt -bin-annot class_type_lib.mli class_type_lib.ml class_type_bin.ml |
| 134 | +dead_code_analyzer --nothing -M all . |
| 135 | +Scanning files... |
| 136 | + [DONE] |
| 137 | +
|
| 138 | +.> UNUSED METHODS: |
| 139 | +================= |
| 140 | +
|
| 141 | +Nothing else to report in this section |
| 142 | +-------------------------------------------------------------------------------- |
| 143 | +
|
| 144 | +
|
| 145 | +make: Leaving directory '/tmp/docs/methods/code_construct/class_type' |
| 146 | +``` |
| 147 | + |
| 148 | +Nothing is reported. Our work here is done. |
0 commit comments