Why doesn't pattern matching on a property of a record compile?

后端 未结 2 1616
借酒劲吻你
借酒劲吻你 2021-01-27 23:49

Why doesn\'t pattern matching on a property of a record compile?

type Cell = { X:int; Y:int }

let isNeighbor cell1 cell2 =
    match cell1.X, c         


        
相关标签:
2条回答
  • 2021-01-28 00:25

    When you do

    match cell1.X, cell2.X with
    | cell1, cell2
    

    you have created a new cell1 variable, which is cell1.X (an int).

    I would probably have just used an if here, or change to | _,_ when ...

    0 讨论(0)
  • 2021-01-28 00:29

    As John Palmer already explained, you are shadowing the original parameters. If pattern matching does not feel well suited if-elif-else might be better. If you want to use pattern matching, the following could be helpful:

    let isNeighbor { X=x1; Y=y1 } { X=x2; Y=y2} =
        match abs(x1-x2), abs(y1-y2) with
        | 0,1 | 1,0 | 1,1 -> true
        | _ -> false
    

    Remove the 1,1 pattern if diagonal adjacent cells should not be neighbors. And then, there is also Euclidean distance...

    0 讨论(0)
提交回复
热议问题