F#如何返回值为元组或null
F#不允许返回null 如何让值以元组或null的形式返回?并不是F#不允许返回null 这是因为零件和其他零件有不同的类型 你可以用 使用F#如何返回值为元组或null,f#,F#,F#不允许返回null 如何让值以元组或null的形式返回?并不是F#不允许返回null 这是因为零件和其他零件有不同的类型 你可以用 使用retVal时,使用模式匹配: let retVal = if reader.Read() then Some (reader.GetString(0), getBytesData reader 1, reader.GetDateTime(2)) else None 为了给尹朱的答案添加一些额外的信息,F#语言中null值的情况如下
retVal
时,使用模式匹配:
let retVal =
if reader.Read() then
Some (reader.GetString(0), getBytesData reader 1, reader.GetDateTime(2))
else
None
为了给尹朱的答案添加一些额外的信息,F#语言中
null
值的情况如下:
- F#类型,如元组(例如,
),这正是您的情况,没有int*int
作为有效值,因此在这种情况下不能使用null
(其他此类类型是函数值,例如null
,列表和大多数F#库类型)int->int
- .NET framework中的类型可以具有
值,因此您可以编写以下示例:null
这不是惯用的F#风格,但它是允许的match retVal with | Some v -> ... | None -> // null case
- 如果您定义自己的F#类型,它将不会自动允许您使用
作为该类型的有效值(其目标是尽量减少在F#中使用null
)。但是,您可以明确允许:null
let (rnd:Random) = null
[] 类型MyType=。。。
Unchecked.Defaultof
如下:
[<AllowNullLiteral>]
type MyType = ...
[<AllowNullLiteral>]
type MyType = ...
let retVal =
if reader.Read() then
(reader.GetString(0), getBytesData reader 1, reader.GetDateTime(2))
else
Unchecked.Defaultof<_>
let retVal =
if reader.Read() then
Some (reader.GetString(0), getBytesData reader 1, reader.GetDateTime(2))
else
None