Search code examples
f#rider

Why am I getting a 'value unused' warning, in a match statement, in F#


I have the following code:

   try
        let s = orderId.Split('|')
        match (s.[0], s.[1]) with
        | "G", coreGuid ->  OrderClass.Grid
        | "C", coreGuid ->  OrderClass.Close
        | _, _          ->  OrderClass.External
    with _ ->
        OrderClass.External

It takes a string in the form of "letter|guid", and then tries to match it. The logic I want to achieve is:

if s.[0] = "G" && s.[1] = coreGuid   for the first line, and
if s.[0] = "C" && s.[1] = coreGuid   for the second line

but my IDE (Rider 2020.1 MacOs) is giving me this warning:

enter image description here

I don't understand why?


Solution

  • It's because in the pattern section the variable is capturing the matched value. This means that whatever the value of s.[1] is, it will be bound to the name coreGuid so it can be used in the right-hand side. The IDE is letting you know that are not using it anywhere.

    It's an helpful error message because your code does not do what you expect. It is ignoring the value of s.[1]. Whenever a variable appears in a pattern, it means that that field can have any value.

    I think this should work as you expect:

        | "G", g when g = coreGuid ->  OrderClass.Grid