gpt4 book ai didi

F# 如何返回具有元组或空值的值

转载 作者:行者123 更新时间:2023-12-03 18:21:00 29 4
gpt4 key购买 nike

    let retVal =
if reader.Read() then
(reader.GetString(0), getBytesData reader 1, reader.GetDateTime(2))
else
null

F# 不允许返回 null

我怎样才能以元组或空值的形式返回值?

最佳答案

并不是 F# 不允许您返回 null。

这是因为 then 部分和 else 部分具有不同的类型。

您可以使用 Option type .

let retVal =
if reader.Read() then
Some (reader.GetString(0), getBytesData reader 1, reader.GetDateTime(2))
else
None

当您使用 retVal ,您使用模式匹配:
match retVal with
| Some v -> ...
| None -> // null case

关于F# 如何返回具有元组或空值的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2983242/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com