7.16 シグネチャ内での代入

(OCaml 3.12 〜)

mod-constraint ::= ...
| type [ type-parameters ] typeconstr-name := [ type-parameters ] typeconstr-name
| module module-name := extended-module-path

「破壊的」代入(with ... :=)は本質的には通常のシグネチャ制約(with ... =)のように振る舞いますが、それに加えて、再定義された型やモジュールをシグネチャから取り除きます。ただし、いくつかの制約があります。まず、いちばん外側のモジュールの型やモジュールしか取り除くことができません(下位モジュールのものは取り除けません)。また、取り除かれる定義は(同一の型パラメータを持つ)型構成子か、モジュールパスでなければなりません。

破壊的代入の自然な例は、型名を共有するシグネチャを併合することです。

module type Printable = sig
  type t
  val print : Format.formatter -> t -> unit
end
module type Comparable = sig
  type t
  val compare : t -> t -> int
end
module type PrintableComparable = sig
  include Printable
  include Comparable with type t := t
end
    

フィールドを完全に取り除いてしまうこともできます。

#module type S = Comparable with type t := int;;
module type S = sig val compare : int -> int -> int end
    

名前を変更することもできます。

#module type S = sig
   type u
   include Comparable with type t := u
 end;;
module type S = sig type u val compare : u -> u -> int end
    

同一の型を代入することで文脈上明らかな型を取り除くこともできます。

#module type ComparableInt = Comparable with type t = int ;;
module type ComparableInt = sig type t = int val compare : t -> t -> int end
#module type CompareInt = ComparableInt with type t := int ;;
module type CompareInt = sig val compare : int -> int -> int end