1

我在 diff 中收到以下错误,在 Subset 下带有红色波浪线。
Type mismatch. Expecting a Range -> Choice but given a Range * Range -> Choice

是否可以将某种类型的注释添加到子集匹配中,这样我就不必使用 fst 和 snd?如果没有,是否有意支持这种语法?

type Range = {min : int64; max : int64}

let (|Before|After|BeforeOverlap|AfterOverlap|SuperSet|SubSet|) (x, y) = 
    if x.min > y.max then After
    elif x.min >= y.min then
        if x.max <= y.max then SubSet
        else AfterOverlap
    elif x.max < y.min then Before
    elif x.max <= y.max then BeforeOverlap
    else SuperSet

let useOldx x xe ye = ()

let diff (xe:IEnumerator<Range>) (ye:IEnumerator<Range>) =
    match xe.Current, ye.Current with
    | After as tuple -> ()
    | Before as t -> if xe.MoveNext() then useOldx (fst t) xe ye
    | SuperSet as t -> 
        let x, y = t
        if xe.MoveNext() then useOldx x xe ye
    | SubSet as x, y -> if xe.MoveNext() then useOldx x xe ye
    | _ -> ()
4

1 回答 1

6

你可以这样做:

    | (x,y) & SubSet -> if xe.MoveNext() then useOldx x xe ye

或者你可以这样做:

open System.Collections.Generic 

type Range = {min : int64; max : int64}

let (|Before|After|BeforeOverlap|AfterOverlap|SuperSet|SubSet|) (x, y) = 
    if x.min > y.max then After
    elif x.min >= y.min then
        if x.max <= y.max then SubSet(x,y)
        else AfterOverlap
    elif x.max < y.min then Before
    elif x.max <= y.max then BeforeOverlap
    else SuperSet

let useOldx x xe ye = ()

let diff (xe:IEnumerator<Range>) (ye:IEnumerator<Range>) =
    match xe.Current, ye.Current with
    | After as tuple -> ()
    | Before as t -> if xe.MoveNext() then useOldx (fst t) xe ye
    | SuperSet as t -> 
        let x, y = t
        if xe.MoveNext() then useOldx x xe ye
    | SubSet(x, y) -> if xe.MoveNext() then useOldx x xe ye
    | _ -> ()
于 2009-11-29T22:06:52.977 回答