Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

This rule will never be matched: Why?

Tags:

f#

I have the following function:

let hasColumnValue<'columnType> columnName (value: 'columnType) (row: KeyValuePair<int, ObjectSeries<string>>) =
    let columnValue =
        Series.get columnName row.Value :?> 'columnType

    match columnValue with
    | value -> true
    | _ -> false

The default pattern matching is flagged as "This rule will never be matched"

=> I don't understand why

Thanks in advance

like image 907
Digital Stoic Avatar asked Feb 22 '26 05:02

Digital Stoic


1 Answers

First pattern always matches because it's binding to variable. If variable with same name exists, it gets shadowed. But, if name refers to literal binding, then pattern succeeds if value equals to underlying constant value

let x = 1
[<Literal>] 
let Three = 3

match 2 with
| Three -> printfn "Three"
| x -> printfn "%d" x 
// prints 2

match 3 with
| Three -> printfn "Three"
| x -> printfn "%d" x 
// prints Three

In your case it will be most clear to use equality instead of pattern matching

 let hasColumnValue<'columnType when 'columnType: equality> columnName (value: 'columnType) (row: KeyValuePair<int, ObjectSeries<string>>) =
    let columnValue =
        Series.get columnName row.Value :?> 'columnType

    columnValue = value
like image 132
JL0PD Avatar answered Feb 25 '26 07:02

JL0PD