|
| 1 | +The reference files for this example are in the |
| 2 | +[polymorphic\_class](../../../examples/docs/methods/code_constructs/polymorphic_class) 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 polymorphic_class build |
| 12 | +``` |
| 13 | + |
| 14 | +The analysis command is : |
| 15 | +``` |
| 16 | +make -C polymorphic_class analyze |
| 17 | +``` |
| 18 | + |
| 19 | +The compile + analyze command is : |
| 20 | +``` |
| 21 | +make -C polymorphic_class |
| 22 | +``` |
| 23 | + |
| 24 | +## First run |
| 25 | + |
| 26 | +Code: |
| 27 | +```OCaml |
| 28 | +(* polymorphic_class_lib.mli *) |
| 29 | +class ['a] stack : object |
| 30 | + method push : 'a -> unit |
| 31 | + method pop : unit |
| 32 | + method peek : 'a option |
| 33 | + method reset : unit |
| 34 | +end |
| 35 | +``` |
| 36 | +```OCaml |
| 37 | +(* polymorphic_class_lib.ml *) |
| 38 | +class ['a] stack = object |
| 39 | + val mutable l : 'a list = [] |
| 40 | +
|
| 41 | + method push x = l <- x::l |
| 42 | +
|
| 43 | + method pop = |
| 44 | + match l with |
| 45 | + | [] -> () |
| 46 | + | _::tl -> l <- tl |
| 47 | +
|
| 48 | + method peek = |
| 49 | + match l with |
| 50 | + | [] -> None |
| 51 | + | hd::_ -> Some hd |
| 52 | +
|
| 53 | + method reset = l <- [] |
| 54 | +end |
| 55 | +``` |
| 56 | +```OCaml |
| 57 | +(* polymorphic_class_bin.ml *) |
| 58 | +let push_n_times n stack = |
| 59 | + for i = 1 to n do |
| 60 | + stack#push i; |
| 61 | + done |
| 62 | +
|
| 63 | +let clear_stack stack = |
| 64 | + while stack#peek <> None do |
| 65 | + stack#pop; |
| 66 | + done |
| 67 | +
|
| 68 | +let () = |
| 69 | + let int_stack = new Polymorphic_class_lib.stack in |
| 70 | + let n = 42 in |
| 71 | + push_n_times n int_stack; |
| 72 | + clear_stack int_stack |
| 73 | +``` |
| 74 | + |
| 75 | +Before looking at the analysis results, let's look at the code. |
| 76 | + |
| 77 | +This example is very similar to the [Class](./CLASS.md) example, without the |
| 78 | +`counter` and `unused_class` classes. Instead of using an definitive `int_stack` |
| 79 | +class, the `Polymorphic_class_lib` defines a polymorphic `['a] stack` class. |
| 80 | + |
| 81 | +Methods `push`, `peek`, and `pop` are used, leaving `reset` as the only unused method. |
| 82 | + |
| 83 | +Compile and analyze: |
| 84 | +``` |
| 85 | +$ make -C polymorphic_class/ |
| 86 | +make: Entering directory '/tmp/docs/methods/code_constructs/polymorphic_class' |
| 87 | +ocamlopt -bin-annot polymorphic_class_lib.mli polymorphic_class_lib.ml polymorphic_class_bin.ml |
| 88 | +dead_code_analyzer --nothing -M all . |
| 89 | +Scanning files... |
| 90 | + [DONE] |
| 91 | +
|
| 92 | +.> UNUSED METHODS: |
| 93 | +================= |
| 94 | +/tmp/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#reset |
| 95 | +
|
| 96 | +Nothing else to report in this section |
| 97 | +-------------------------------------------------------------------------------- |
| 98 | +
|
| 99 | +
|
| 100 | +make: Leaving directory '/tmp/docs/methods/code_constructs/polymorphic_class' |
| 101 | +``` |
| 102 | + |
| 103 | +As expected, the analyzer reports `stack#reset` as unused. |
| 104 | + |
| 105 | +> [!NOTE] |
| 106 | +> The analyzer does not specify the type parameter of `stack`. This is because |
| 107 | +> it does not distinguish the monomorphizations of `stack` and its polymoprhic |
| 108 | +> definition. |
| 109 | +
|
| 110 | +After removing the reported unused methods, the analyzer will not find anymore |
| 111 | +unused method. Our work here is done. |
0 commit comments