F#:可空支持

发布于 2024-07-23 22:25:58 字数 352 浏览 3 评论 0原文

在 F# 中使用 Nullable 的正确方法是什么?

目前我正在使用这个,但它看起来非常混乱。

let test (left : Nullable<int>) = if left.HasValue then left.Value else 0

Console.WriteLine(test (new System.Nullable<int>()))
Console.WriteLine(test (new Nullable<int>(100)))

let x = 100
Console.WriteLine(test (new Nullable<int>(x)))

What is the right way to use Nullable in F#?

Currently I'm using this, but it seems awefully messy.

let test (left : Nullable<int>) = if left.HasValue then left.Value else 0

Console.WriteLine(test (new System.Nullable<int>()))
Console.WriteLine(test (new Nullable<int>(100)))

let x = 100
Console.WriteLine(test (new Nullable<int>(x)))

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

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

发布评论

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

评论(2

红焚 2024-07-30 22:25:58

恐怕 F# 中没有可空类型的语法糖(与 C# 不同,在 C# 中您只需将 ? 附加到类型)。 是的,您在那里显示的代码确实看起来非常冗长,但这是在 F# 中使用 System.Nullable 类型的唯一方法。

但是,我怀疑您真正想要使用的是 选项类型。 MSDN 页面上有一些不错的示例:

let keepIfPositive (a : int) = if a > 0 then Some(a) else None

并且

open System.IO
let openFile filename =
   try
       let file = File.Open (filename, FileMode.Create)
       Some(file)
   with
       | exc -> eprintf "An exception occurred with message %s" exc.Message; None 

显然使用起来要好得多!

选项本质上履行了 F# 中可空类型的作用,我认为您确实希望使用它们而不是可空类型(除非您正在与 C# 进行互操作)。 实现上的差异在于,选项类型由 Some(x)None可区分联合构成,而 Nullable; 是 BCL 中的一个普通类,在 C# 中带有一些语法糖。

I'm afraid there's no syntactical sugar for nullable types in F# (unlike in C# where you simply append a ? to the type). So yeah, the code you show there does look terribly verbose, but it's the only way to use the System.Nullable<T> type in F#.

However, I suspect what you really want to be using are option types. There's a few decent examples on the MSDN page:

let keepIfPositive (a : int) = if a > 0 then Some(a) else None

and

open System.IO
let openFile filename =
   try
       let file = File.Open (filename, FileMode.Create)
       Some(file)
   with
       | exc -> eprintf "An exception occurred with message %s" exc.Message; None 

Clearly a lot nicer to use!

Options essentially fulfill the role of nullable types in F#, and I should think you really want to be using them rather than nullable types (unless you're doing interop with C#). The difference in implementation is that option types are formed by a discriminated union of Some(x) and None, whereas Nullable<T> is a normal class in the BCL, with a bit of syntactical sugar in C#.

少女净妖师 2024-07-30 22:25:58

您可以让 F# 推断其中的大多数类型:

let test (left : _ Nullable) = if left.HasValue then left.Value else 0

Console.WriteLine(test (Nullable()))
Console.WriteLine(test (Nullable(100)))

let x = 100
Console.WriteLine(test (Nullable(x)))

您还可以使用活动模式来应用可空类型的模式匹配:

let (|Null|Value|) (x: _ Nullable) =
    if x.HasValue then Value x.Value else Null

let test = // this does exactly the same as the 'test' function above
    function
    | Value v -> v
    | _ -> 0

我不久前在博客中介绍了 F# 中的可空类型 [/无耻_插件]

You can let F# infer most of the types there:

let test (left : _ Nullable) = if left.HasValue then left.Value else 0

Console.WriteLine(test (Nullable()))
Console.WriteLine(test (Nullable(100)))

let x = 100
Console.WriteLine(test (Nullable(x)))

You can also use an active pattern to apply pattern matching on nullable types:

let (|Null|Value|) (x: _ Nullable) =
    if x.HasValue then Value x.Value else Null

let test = // this does exactly the same as the 'test' function above
    function
    | Value v -> v
    | _ -> 0

I blogged some time ago about nullable types in F# [/shameless_plug]

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