簡體   English   中英

如何在子模塊中使用模塊中創建的異常(在ocaml中)

[英]How to use in a submodule a exception created in module (in ocaml)

我有一個模塊類型A ,但有例外。 A將由BC實施

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

我不明白為什么它不起作用。

AB需要履行的簽名。 在您的上下文中,這意味着您必須再次編寫聲明行:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM