3 (*****************************************************************************)
4 (* TypeClass via module signature. *)
5 (*****************************************************************************)
7 * Use this not so much for functors, I hate functors, but
8 * more to force me to have consistent naming of stuff.
10 * It's related to objet.ml in some way, but use a different scheme.
12 * src: (strongly) inspired by Jane Street core lib, which in turn
13 * may have been strongly inspired by Java Interfaces or Haskell
18 * Example of use in .mli:
21 * include Stringable with type stringable = t
22 * include Comparable with type comparable = t
24 * Example of use in .ml:
27 * type stringable = xxx
28 * let of_string = bool_of_string
29 * let to_string = string_of_bool
32 * No this file is not about (graphical) user interface. See gui.ml for that.
35 * todo? but as in type class, or object, can not have default method
41 (*****************************************************************************)
43 (*****************************************************************************)
45 (* note: less need for cloneable, copyable as in Java. Only needed
46 * when use ref, but refs should be avoided anyway so better not to
49 * Often found this in haskell:
51 * data x = ... deriving (Read, Show, Eq, Ord, Enum, Bounded)
53 * Apparently this is what is considered basic by haskell.
57 module type Check_able
= sig
59 val invariant
: checkable
-> unit (* raise exception *)
64 (* Normally should not use the '=' of ocaml. cf common.mli on this issue. *)
65 module type Eq_able
= sig
67 val equal
: eqable
-> eqable
-> bool
68 (* Jane Street have far more (complex) stuff for this typeclass *)
70 val (=*=): eqable
-> eqable
-> bool
75 (* Same, should not use compare normally, dangerous when evolve code.
76 * Called Ord in haskell. Inherit Eq normally.
78 module type Compare_able
= sig
80 val compare
: compareable
-> compareable
-> bool
82 (* Jane street have also some binable, sexpable *)
85 (* Haskell have lots of related type class after Num such as
86 * Real, Fractional, Integral, RealFrac, Floating, RealFloat
88 module type Num_able
= sig
95 (*****************************************************************************)
96 (* Show/read related *)
97 (*****************************************************************************)
100 (* Called show/read in haskell *)
101 module type String_able
= sig
103 val of_string : string -> stringable
104 val to_string : stringable
-> string
107 module type Debug_able
= sig
109 val debug
: debugable
-> string
113 module type XML_able
= sig
115 val of_xml
: string -> xmlable
116 val to_xml
: xmlable
-> string
118 (* Jane street have also some BIN_able, and SEXP_able (but no sex_able) *)
120 module type File_able
= sig
122 val load
: filename
-> fileable
123 val save
: fileable
-> filename
-> unit
126 (* a.k.a Marshall_able *)
127 module type Serialize_able
= sig
129 val serialize
: serializeable
-> string
130 val unserialize
: string -> serializeable
134 module type Open_able
= sig
136 val openfile
: filename
-> openable
137 val close
: openable
-> unit
140 (*****************************************************************************)
142 (*****************************************************************************)
144 (* This is related to ocollection.ml in some way, but use a different scheme *)
146 (* Require Constructor class ? So can not do it ? apparently can. Note the
147 * 'b which is not declareted but seems to pose no problem to ocamlc.
149 module type Map_able
= sig
151 val map
: ('a
-> 'b
) -> 'a mapable
-> 'b mapable
154 module type Iter_able
= sig
156 val iter
: ('a
-> unit) -> 'a iterable
-> unit
160 (* testable ? actionable ? *)
164 (* monad ? functor *)
168 (*****************************************************************************)
169 (* Idea taken from Jane Street Core library, slightly changed.
171 * It's another way to organize data structures, module instead of objects.
172 * It's also the Java way.
174 * It makes some code looks a little bit like Haskell* typeclass.
178 (* In Jane Street they put each interface in its own file but then have to
181 * module type Stringable = Stringable.S
182 * module type Comparable = Comparable.S
183 * module type Floatable = Floatable.S
184 * module type Hashable = Hashable.S
185 * module type Infix_comparators = Comparable.Infix
186 * module type Monad = Monad.S
187 * module type Robustly_comparable = Robustly_comparable.S
188 * module type Setable = Setable.S
189 * module type Sexpable = Sexpable.S
190 * module type Binable = Binable.S
192 * And I dont like having too much files, especially as all those xxable
193 * end with able, not start, so don't see them together in the directory.