2016-12-31 36 views
0

我想写一个函数接受自定义类myType的值,并返回myType option。不确定我的问题是否与签名,内容或返回值有关。OCaml选项返回值和选项匹配

例如,我试着写以下(它的简化,并没有实际意义):

let rec myFunc (t:myType) myType option = 
    let t2 = myFunc t in 
    match t2 with 
    | None -> None 
    | _ -> t 

而且我得到以下编译错误:

Error: This pattern matches values of type 'a option but a pattern was expected which matches values of type 'b -> 'c -> 'd

不确定我的语法有什么问题或我误解OCaml的地方。

回答

5

我只看到一个失踪冒号和Some

let rec myFunc (t:myType): myType option = 
    let t2 = myFunc t in 
    match t2 with 
    | None -> None 
    | _ -> Some t 

稍微精简版:

let rec myFunc (t:myType): myType option = 
    match myFunc t with 
    | None -> None 
    | _ -> Some t 
+0

它看起来像一个无限循环给我。 – RichN

+0

当然是。 OP说:(它简化了,没有实际意义) –