我在OCaml中有自定义类型:
type d = DefaultTemplateDir | TemplateDir of string
稍后在程序中我应用了这个:
let get_full_name ?(template_dir: d = DefaultTemplateDir) file_name =
let dir = (match template_dir with
| DefaultTemplateDir -> ""
| TemplateDir custom_dir -> custom_dir) in
dir ^ file_name
但我在这里收到错误:
This expression has type d but an expression was expected of type string.
如何将TemplateDir类型转换回字符串,以便对其进行字符串操作?
答案 0 :(得分:2)
您的代码看起来是正确的。你需要将其称为
get_full_name ~template_dir:(TemplateDir "template-dir") "file-name"