Download Introduction to the Objective Caml Programming Language
Transcript
6 CONTENTS 10.3 Some common errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 10.3.1 Interface errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 10.4 Using open to expose a namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 10.4.1 A note about open . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 10.5 Debugging a program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 11 The OCaml Module System 113 11.1 Simple modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 11.2 Module definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 11.2.1 Using include to extend modules . . . . . . . . . . . . . . . . . . . . . . . . . 117 11.2.2 Using include to extend implementations . . . . . . . . . . . . . . . . . . . . 117 11.3 Abstraction, friends, and module hiding . . . . . . . . . . . . . . . . . . . . . . . . . 118 11.3.1 Using include with incompatible signatures . . . . . . . . . . . . . . . . . . . 120 11.4 Sharing constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 11.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 11.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 12 Functors 127 12.1 Sharing constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 12.2 Module re-use using functors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 12.3 Higher-order functors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 12.4 TODO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 13 The OCaml Object System 137 13.1 Simple classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 13.1.1 Objects vs. classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 13.2 Parameterized classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 13.3 Self references and private methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 13.4 Class initializers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 13.4.1 Let-initializers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 13.4.2 Anonymous initializer methods . . . . . . . . . . . . . . . . . . . . . . . . . . 144 13.5 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145