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

发布于 2024-08-12 14:41:54 字数 946 浏览 6 评论 0原文

我在 diff 中收到以下错误,并在子集下出现红色曲线。
类型不匹配。期望范围 ->选择但给定范围 * 范围 -> 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
    | _ -> ()

I get the following error in diff with a red squiggle under Subset.
Type mismatch. Expecting a Range -> Choice but given a Range * Range -> Choice

Is there some sort of type annotation I can add to the SubSet match so I don't have to use fst and snd? If not is there any intention to support this syntax?

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
    | _ -> ()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

郁金香雨 2024-08-19 14:41:54

你可以这样做:

    | (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
    | _ -> ()

You could do this:

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

or you could do this:

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
    | _ -> ()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文