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/