OCaml option return value and option matching

1.5k views Asked by At

I want to write a function that accepts values of a custom class myType, and returns myType option. Not sure if my problem is with signature, content or return values.

For example, I've tried to write the following (it's simplified and have no real meaning):

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

And I'm getting the following compilation error:

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

Not sure what's wrong with my syntax or where I'm misunderstanding OCaml.

1

There are 1 answers

2
Jeffrey Scofield On BEST ANSWER

I only see a missing colon and Some:

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

Slightly streamlined version:

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