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

标签 f#

    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/

相关文章:

f# - 使用静态解析的类型参数链接中缀运算符

f# - 有没有更好的方法在 F# 中编写命名管道?

silverlight - 使用 F# 创建 Silverlight 3 应用程序

.net - 没有警告错误的沮丧工会案例

compiler-construction - 从哪里获得 F# ILX2CIL 汇编程序?

f# - 在 F# 中从联合中随机选择一个实例

f# - 尝试运行用 F# 编写的应用程序时出现 System.IO.FileNotFoundException 异常

f# - 如何在 F# 中异步下载网页并捕获错误

f# - 在 ILMerge 中允许重复的泛型类型

f# - 范围分配