F#将活动模式匹配为扩展元组

时间:2009-11-29 21:56:47

标签: f# match tuples active-pattern

我在差异中得到以下错误,并且在子集下有红色波浪线 Type mismatch. Expecting a Range -> Choice but given a Range * Range -> Choice

是否有某种类型的注释可以添加到SubSet匹配中,所以我不必使用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
    | _ -> ()

1 个答案:

答案 0 :(得分: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
    | _ -> ()