Right now, I have a nested pattern match that is behaving oddly.
Here is the code snippet
match setting with
| "-f" | "--file" -> // Open file
({ state with file = Some (cur_dir + "\\" + elem) }, "")
| "" -> // Option without parameter
match elem with
| "-h" | "--help" -> // Display help message
({ state with help = true }, "")
| "-r" | "--readonly" -> // Open with readonly
({ state with readonly = true }, "")
| opt -> (state, opt)
| _ -> (state, "") // Ignore invalid option
Unfortunately, I get two warnings when I compile the above code. One states that the final wildcard pattern is unreachable. The other states that the first match statement is incomplete.
These two warnings seem to be in conflict with each other, and if I add in parentheses to make the expression more clear, the warnings disappear and my code works correctly.
match setting with
| "-f" | "--file" -> // Open file
({ state with file = Some (cur_dir + "\\" + elem) }, "")
| "" -> // Option without parameter
( match elem with
| "-h" | "--help" -> // Display help message
({ state with help = true }, "")
| "-r" | "--readonly" -> // Open with readonly
({ state with readonly = true }, "")
| opt -> (state, opt) )
| _ -> (state, "") // Ignore invalid option
My question is this: what was wrong with the original code? It was not a case of a misplaced parenthesis, and it seems like it should be obvious to the compiler where each match ends.
Edit: here's the complete function, in case it helps
let process_args argv =
let (result, _) = ((default_settings, "-f"), argv) ||>
Array.fold (fun (state, setting) elem ->
match setting with
| "-f" | "--file" -> // Open file
({ state with file = Some (cur_dir + "\\" + elem) }, "")
| "" -> // Option without parameter
match elem with
| "-h" | "--help" -> // Display help message
({ state with help = true }, "")
| "-r" | "--readonly" -> // Open with readonly
({ state with readonly = true }, "")
| opt -> (state, opt)
| _ -> (state, "") // Ignore invalid option
) in
result
I should also note that this error occurs with both light and verbose syntax.
Edit2: I mistakenly pasted a slightly altered function, which resulted in some compilation errors instead of just warnings. My apologies.
#light "off"- idlysprocess_argsfunction, I get an indentation error on theletstatement. I'll write a full answer, but the short version (so you don't have to wait) is: move your((default_settings, etc.tuple down to the next line and indent it four spaces, and make no other changes, and your code should work. - rmunn