详情请参阅My previous question
1) cpf0.ml:
type string = char list
type name = string
type symbol =
| Symbol_name of name
2) problem.ml:
type symbol =
| Ident of Cpf0.string
在此 problem.ml
中,它有两个 string
类型的定义,当然它给了我一个错误,但我是否可以使它们具有相同的定义类型?我需要一个想法。
module Str = struct type t = string end;;
module StrOrd = Ord.Make (Str);;
module StrSet = Set.Make (StrOrd);;
module StrMap = Map.Make (StrOrd);;
module SymbSet = Set.Make (SymbOrd);;
let rec ident_of_symbol = function
| Ident s -> s
let idents_of_symbols s =
SymbSet.fold (fun f s -> StrSet.add (ident_of_symbol f) s) s StrSet.empty;;
此表达式的类型为 Cpf0.string = char list,但表达式的类型应为 Util.StrSet.elt = string
最佳答案
如果您愿意,您可以在不同模块中使用名称“string”来表示不同类型,但(正如 Basile Starynkevitch 指出的那样)这很令人困惑。最好选择一个不同的名字。如果确实需要重用该名称,则可以每次都指定模块。如果您不指定模块,您将获得预定义的含义(或最里面打开的模块的含义)。
在我看来,您引用的代码中的问题在于这一行:
module Str = struct type t = string end;;
没有为字符串指定模块名称,因此它引用预定义的字符串
。您似乎可能想说:
module Str = struct type t = Cpf0.string end;;
但是,这很难说。没有足够的上下文让我真正理解你想要做什么。
关于ocaml - 想法在类型中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10052230/