[英]How to use in a submodule a exception created in module (in ocaml)
我有一個模塊類型A
,但有例外。 A
將由B
和C
實施
module type A = sig
type t
val f: t->t->t
exception DivisionParZero
end
module B : A = struct
type t = int
let f a b=
if (a==0) then raise DivisionParZero else b/a
end
end
Ocamlc在編譯B時說:
錯誤:此變體表達式應具有類型exn構造函數DivisionParZero不屬於類型exn
我不明白為什么它不起作用。
A
是B
需要履行的簽名。 在您的上下文中,這意味着您必須再次編寫聲明行:
module B : A = struct
type t = int
exception DivisionParZero
let f a b=
if (a==0) then raise DivisionParZero else b/a
end
您可以通過返回隨機值而不是引發異常來進行一些實驗,您將看到編譯器告訴您實現不符合簽名:
Error: Signature mismatch:
Modules do not match:
sig type t = int val f : int -> int -> int end
is not included in
A
The extension constructor `DivisionParZero' is required but not provided
File "test.ml", line 4, characters 2-27: Expected declaration
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.