我的幾個模塊包含實現給定類類型有兩種方法,private_method
和public_method
全局類實例。模塊簽名Class實例類型強制
我想MyModule.my_instance # public_method
可以在我的程序中的任何地方使用,MyModule.my_instance # private_method
只能在MyModule
內使用。
我已經嘗試以下操作:
class type public_type = object
method public_method : int
end ;;
class type private_type = object
method public_method : int
method private_method : int
end ;;
let make_private : unit -> private_type = fun() -> object
method public_method = 0
method private_method = 0
end ;;
module type MY_MODULE = sig
val my_instance : public_type
end
module MyModule : MY_MODULE = struct
let my_instance = make_private()
let _ = print_int (my_instance # private_method)
end
然而,這將導致一個錯誤:
Values do not match:
val my_instance : private_type
is not included in
val my_instance : public_type
我可以寫手動脅迫:
module MyModule : MY_MODULE = struct
let my_instance = make_private()
let _ = print_int (my_instance # private_method)
let my_instance = (my_instance :> public_type)
end
但我'而不是將代碼大小加倍,以獲得簡單的答案這是。
你對爲什麼出現這種情況有什麼建議,我怎麼能解決呢?
我有點希望明確強迫MyModule中以MY_MODULE將作爲行爲,這可以用一個明確的對象類型的表達式中使用在... – 2011-01-10 16:16:33