ppx_overload

AuthorJun Furuse
Homepagehttps://bitbucket.org/camlspotter/compiler-libs-hack
Issue Trackerhttps://bitbucket.org/camlspotter/compiler-libs-hack/issues?status=new&status=open
Maintainerjun.furuse@gmail.com
Dependencies
ocamlfind
Availableocaml-version >= "4.02.0" & ocaml-version < "4.03.0"
PublishedNov 5, 2014
Source [http] https://bitbucket.org/camlspotter/compiler-libs-hack/get/1.0.1.tar.gz
ae9d28330b4c7b4e0feec2edf814a8a7
StatisticsNot installed in the last month.
Edithttps://github.com/ocaml/opam-repository/tree/master/packages/ppx_overload/ppx_overload.1.0.1/opam

ppx_overload: SML style simple but user definable overloading

ppx_overload provides SML style overloading of values and functions. This is not so powerful as the type classes, i.e., the overloaded instances must be resolved locally and you cannot inherit overloading by let-polymorphism. But it is very simple but useful.

Overloaded value must be declared by external <name> : <type> = "%OVERLOADED". The type should be the least general anti-unifier of values overloaded. Then its instances should be defined in sub-modules of this external declaration with the same name. Instance types must be less general than the type at the external declaration. Here is an example of overloaded (+):

module Plus = struct
  external (+) : 'a -> 'a -> 'a = "%OVERLOADED"
  module Int = struct
    let (+) = Pervasives.(+)
  end
  module Float = struct
    let (+) = Pervasives.(+.)
  end
end

The use of an overloaded value is replaced by one of its instances depending of its typing. If there is no match or if there are more than one match, this overload resolution fails:

Plus.(+) 1   2;    (* converted to Int.(+) 1 2 *)
Plus.(+) 1.2 3.4;  (* converted to Float.(+) 1.2 3.4 *)

You cannot derive overloading by let-polymorphism:

let double x = Plus.(+) x x (* error *)

You can extend the overloading pretty easily by module inclusion:

module Plus' = struct
  include Plus
  module String = struct
    let (+) = String.(^)
end

Plus'.(+) 1   2;    (* converted to Int.(+) 1 2 *)
Plus'.(+) 1.2 3.4;  (* converted to Float.(+) 1.2 3.4 *)
Plus'.(+) "x" "y";  (* converted to String.(+) "x" "y" *)

Limitation

ppx_overload does not work with toplevel.

No package is dependent