Ocaml 重写变量时如何访问全局构造函数?
我有这个密码Ocaml 重写变量时如何访问全局构造函数?,ocaml,Ocaml,我有这个密码 module Ignore = struct type double_asterisk = | None | Leading of string | Trailing of string | Middle of string * string let double_asterisk (line:string) : double_asterisk = let open Re in let matches pat line =
module Ignore = struct
type double_asterisk =
| None
| Leading of string
| Trailing of string
| Middle of string * string
let double_asterisk (line:string) : double_asterisk =
let open Re in
let matches pat line =
exec_opt pat line in
let middle = matches @@ Perl.compile_pat {|^(.*?)/*\*\*/*(.*?)$|} in
let leading = matches @@ Perl.compile_pat {|^\*\*/*(.*)$|} in
let trailing = matches @@ Perl.compile_pat {|^(.*?)/*\*\*$|} in
let open Re.Group in
match trailing line with
| Some group -> Trailing (get group 1)
| None -> match leading line with
| Some group -> Leading (get group 1)
| None -> match middle line with
| Some group -> Middle ((get group 1), (get group 2))
| None -> None
let%test _ = double_asterisk "**/foo" = Leading "foo"
let%test _ = double_asterisk "foo/**" = Trailing "foo"
let%test _ = double_asterisk "foo/**/bar" = Middle ("foo", "bar")
let getpath filename =
let open Base in
let open Re.Perl in
let open Re in
match exec_opt (compile_pat {|^(.*)/|}) filename with
| None -> None
| Some g -> Some (Re.Group.get g 1) (* <- ERROR HEERE *)
let%test _ = getpath "foo/bar/tar" = Some "foo/bar"
end
自4.08起,您可以将其限定为
选项。无
在早期版本中,您可以自己创建名称空间别名,例如:
module MyOption = struct
type 'a t = 'a option = None | Some of 'a
end
let _ = MyOption.None
但我强烈建议不要重写内置类型的构造函数。考虑使用<代码> No.< /代码>或其他一些等价的术语。
module MyOption = struct
type 'a t = 'a option = None | Some of 'a
end
let _ = MyOption.None