F#Options选项

F#Options选项 首页 / F#入门教程 / F#Options选项

当变量或函数的值可能不存在时,F#中的Options类型将用于计算,Options类型用于表示计算中的可选值,它们可以有两个可能的值- Some(x)或 None 。

使用可选项

以除法函数为例,以下程序对此进行了解释- 编写一个函数div,并向其发送两个参数20和5-

let div x y = x/y
let res = div 20 5
printfn "Result: %d" res

编译并执行程序时,将产生以下输出-

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/fsharp/fsharp-options.html

来源:LearnFk无涯教程网

Result: 4

如果第二个参数为零,则程序将引发异常-

let div x y = x/y
let res = div 20 0
printfn "Result: %d" res

编译并执行程序时,将产生以下输出-

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/fsharp/fsharp-options.html

来源:LearnFk无涯教程网

Unhandled Exception:
System.DivideByZeroException: Division by zero

在这种情况下,可以使用Options类型在操作成功时返回Some(值),在操作失败时返回None。

以下示例演示了Options的使用-

let div x y =
   match y with
   | 0 -> None
   | _ -> Some(x/y)

let res : int option = div 20 4
printfn "Result: %A " res

编译并执行程序时,将产生以下输出-

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/fsharp/fsharp-options.html

来源:LearnFk无涯教程网

Result: Some 5

属性和方法

属性或方法类型说明
None'Toptions一个静态属性,使您可以创建具有无值的Options值。
IsNone bool如果Options具有 None 值,则返回 true 。
IsSome bool如果Options的值不是 None ,则返回 true 。
Some'Toptions创建一个Options的静态成员,该Options的值不是 None 。
Value'T返回基础值,或者,如果该值为 None ,则抛出NullReferenceException。

示例1

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

let res : int option = checkPositive(-31)
printfn "Result: %A " res

编译并执行程序时,将产生以下输出-

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/fsharp/fsharp-options.html

来源:LearnFk无涯教程网

Result: <null>

示例2

let div x y =
   match y with
   | 0 -> None
   | _ -> Some(x/y)

let res : int option = div 20 4
printfn "Result: %A " res
printfn "Result: %A " res.Value

编译并执行程序时,将产生以下输出-

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/fsharp/fsharp-options.html

来源:LearnFk无涯教程网

Result: Some 5
Result: 5

示例3

let isHundred = function
   | Some(100) -> true
   | Some(_) | None -> false

printfn "%A" (isHundred (Some(45)))
printfn "%A" (isHundred (Some(100)))
printfn "%A" (isHundred None)

编译并执行程序时,将产生以下输出-

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/fsharp/fsharp-options.html

来源:LearnFk无涯教程网

false
true
false

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

教程推荐

手把手教你落地DDD -〔钟敬〕

大厂设计进阶实战课 -〔小乔〕

如何讲好一堂课 -〔薛雨〕

To B市场品牌实战课 -〔曹林〕

分布式系统案例课 -〔杨波〕

.NET Core开发实战 -〔肖伟宇〕

人人都能学会的编程入门课 -〔胡光〕

Java性能调优实战 -〔刘超〕

许式伟的架构课 -〔许式伟〕

好记忆不如烂笔头。留下您的足迹吧 :)