Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Module and Class in OCaml

Tags:

ocaml

What are the differences between Module and Class in OCaml.

From my searching, I found this:

Both provide mechanisms for abstraction and encapsulation, for subtyping (by omitting methods in objects, and omitting fields in modules), and for inheritance (objects use inherit; modules use include). However, the two systems are not comparable. On the one hand, objects have an advantage: objects are first-class values, and modules are not—in other words, modules do not support dynamic lookup. On the other hand, modules have an advantage: modules can contain type definitions, and objects cannot.

First, I don't understand what does "Modules do not support dynamic lookup" mean. From my part, abstraction and polymorphism do mean parent pointer can refer to a child instance. Is that the "dynamic lookup"? If not, what actually dynamic lookup means?

In practical, when do we choose to use Module and when Class?

like image 555
yjasrc Avatar asked Jul 16 '13 16:07

yjasrc


People also ask

What is a module in OCaml?

ocaml modules allow to package together data structures definitions and functions operating on them. This allow to reduce code size and name confusion, as any identifier can appear in different modules, with different meanings, without any interference.

What is class and module?

A class is more of a unit, and a module is essentially a loose collection of stuff like functions, variables, or even classes. In a public module, classes in the project have access to the functions and variables of the module. You don't have to specify the module name to address one.

Does OCaml have classes?

OCaml Classes Like module types, class types are completely separate from regular OCaml types (e.g., int , string , and list ) and, in particular, should not be confused with object types (e.g., < get : int; .. > ). The class type describes the class itself rather than the objects that the class creates.

What are Functors in OCaml?

A functor is a module that is parametrized by another module, just like a function is a value which is parametrized by other values, the arguments. It allows one to parametrize a type by a value, which is not possible directly in OCaml without functors.


1 Answers

The main difference between Module and Class is that you don't instantiate a module.

A module is basically just a "drawer" where you can put types, functions, other modules, etc... It is just here to order your code. This drawer is however really powerful thanks to functors.

A class, on the other hand, exists to be instantiated. They contains variables and methods. You can create an object from a class, and each object contains its own variable and methods (as defined in the class).

In practice, using a module will be a good solution most of the time. A class can be useful when you need inheritance (widgets for example).

like image 162
ChristopheLec Avatar answered Oct 13 '22 02:10

ChristopheLec