通过简单的F#模式匹配转换,是否可以在没有警告的情况下忽略不匹配的值? [英] Is it possible, with simple F# pattern matching transformations, to ignore unmatched values without a warning?

查看:197
本文介绍了通过简单的F#模式匹配转换,是否可以在没有警告的情况下忽略不匹配的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我之前曾问过这个问题:

So, I previously asked this question:

在此特定示例(IP地址表达式)中,有人可以帮助我比较使用F#和C#吗?

我正在查看发布的代码,我想知道是否可以在不产生警告的情况下编写此代码:

I was looking at the posted code and I was wondering if this code could be written without it producing a warning:

let [|a;b;c;d|] = s.Split [|'.'|]
IP(parseOrParts a, parseOrParts b, parseOrParts c, parseOrParts d)

是否可以为忽略的匹配_模式做些什么?是否不添加活动模式等内容?我想使代码尽可能简单...我可以在不大幅度更改此代码的情况下做到这一点吗?

Is it possible to do something for the match _ pattern ot ignore? Without adding in something like Active Patterns? i want to keep the code as simple as possible... can I do this without drastically changing this code?

注意:警告如下

警告此表达式上的不完整模式匹配.例如,值'[| _; _; _; _; _ |]'可能表示模式未涵盖的情况.

Warning Incomplete pattern matches on this expression. For example, the value '[|_; _; _; _; _|]' may indicate a case not covered by the pattern(s).

推荐答案

您可以尝试

#nowarn "25"

在部分函数之前.

但是您当然要禁用警告,并且我认为这会针对整个文件将其关闭.我记得曾经看到过一种仅对文件的一部分禁用警告的方法,但是我现在找不到.

But of course you're disabling a warning, and I think this turns it off for the whole file. I remember seeing a way to disable warnings for only part of a file, but I can't find it right now.

还有一个编译器选项--nowarn:25,但这甚至更糟,因为它适用于整个项目.

There is also the compiler option --nowarn:25, but this is even worse since it applies to the whole project.

要正确执行此操作,您需要将三行替换为三行:

To do this the Right Way, you need to replace your two lines with three:

match Array.map parseOrParts (s.Split [|'.'|]) with
| [|a;b;c;d|] -> IP(a,b,c,d)
| _ -> failwith "Oh no!"   // preferably, your exception of choice goes here.

这篇关于通过简单的F#模式匹配转换,是否可以在没有警告的情况下忽略不匹配的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆