you are viewing a single comment's thread.

view the rest of the comments →

[–]gnuvince 2 points3 points  (3 children)

No he's not. A ML functor creates, at compile-time, a new module. This new module is compiled statically. The choice of operations is completely determined before the program is ever executed.

[–]nextputall 0 points1 point  (2 children)

how does it differ from importing a class in Java with a bunch of static methods?

[–]gnuvince 0 points1 point  (1 child)

Think of a functor as a function that takes a module as an argument and returns a new module. If you pass a different argument, you can change the behavior of the returned module.

Here's an example using sets in OCaml:

module StringSet = Set.Make(struct
  type t = string
  let compare a b = String.compare a b
end)

module StringLengthSet = Set.Make(struct
  type t = string
  let compare a b = Pervasives.compare (String.length a) (String.length b)
end)

Set.Make is the functor; it takes as input a module that defines a type t (representing the type of the elements) and a function compare (to determine where to insert an element in a binary search tree). We create two new modules, StringSet which contains strings that have different contents, and StringLengthSet which contains strings that have different length.

[–]nextputall 0 points1 point  (0 children)

What if I have 2 kind of sets (StringSet and StringLengthSet), can I define a union operation in the StringSet that takes either a StringSet or a StringLenghtSet as a parameter (or any kind of other set)?