Jas*_*Yeo 4 ocaml ml code-readability
OCaml的选项类型在您的函数可能不返回任何内容的情况下非常有用.但是当我在很多地方使用它时,我发现在一直处理Some案件和None案件时很麻烦match ... with.
例如,
let env2 = List.map (fun ((it,ie),v,t) ->
match t with
| Some t -> (v,t)
| None ->
begin
match it with
| Some it -> (v,it)
| None -> failwith "Cannot infer local vars"
end) ls_res in
Run Code Online (Sandbox Code Playgroud)
有没有其他方法可以简洁地解构选项类型?
对于简单的情况,您可以同时匹配多个内容:
match t, it with
| Some t, _ -> (v, t)
| None, Some it -> (v, it)
| None, None -> failwith "Cannot infer local vars"
Run Code Online (Sandbox Code Playgroud)
这是我一直在做的事情.我被告知编译器对这个构造很好(它实际上并没有产生额外的对).