Why is this F # nesting matching expression not working correctly?

Right now, I have a nested pattern match that behaves strangely.

Here is a snippet of code

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 compiling the above code. One claims that the final template template is not available. Another states that the first matching statement is incomplete.

These two warnings seem to contradict each other, and if I add in parentheses to make the expression more understandable, 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: what happened to the source code? This was not a case of an incorrect bracket, and it seems that this should be obvious to the compiler, where each match ends.

Edit: here is the full function if 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

, , .

Edit2: , . .

+4
1

VS, , :

let process_args argv =
    let (result, _) = ((default_settings, "-f"), argv)
        ||> Array.fold (fun (state, setting) elem ->
        // (snip rest of function)

let let (result, _) = ... ||>. let:

. , , "let".

||>:

. .

, , , - ||>.

F #, "pipe" (|>, ||> ..) , , . ||> (result, _). ||>; . ||> , ((default_settings, "-f"), argv). , :

let process_args argv =
    let (result, _) = ((default_settings, "-f"), argv)
                      ||> Array.fold (fun (state, setting) elem ->
                      // (snip rest of function)

:

let process_args argv =
    let (result, _) =
        ((default_settings, "-f"), argv)
        ||> Array.fold (fun (state, setting) elem ->
        // (snip rest of function)

, .

+4

Source: https://habr.com/ru/post/1651345/


All Articles