模块中函数声明的常见签名是最后一个参数具有主状态(Module.t)的类型。就像在“列表”模块中一样。此表单打开了使用“|>”运算符的能力,例如:
[1;2;3] |> List.filter ((>)2)
|> List.map ((-)1)
|> List.fold_left 0 (+)
但是“选项”模块中的“绑定”函数不遵循这种形式。它具有“Option.t”参数作为第一个
val bind : 'a option -> ('a -> 'b option) -> 'b option
但是,好吧,我可以改变它。我用相反的参数顺序声明了函数'opt_bind'。
let opt_bind = Fun.flip Option.bind
但是这个不行。并且以下代码编译时出现以下错误
type a = A of int
type b = B of int
let f x = Some (A x)
let g (A x) = Some (B x)
let opt_bind = Fun.flip Option.bind
let result =
(Some 42) |> opt_bind f
|> opt_bind g
|> opt_bind g ^
错误:此表达式具有类型 a -> b 选项,但表达式应为 > type int -> a 选项。a 类型与 int 类型不兼容
同样的情况与
let result =
let x = opt_bind f (Some 42) in
let x = opt_bind g x in
x
即使在我注意到所有类型之后,我仍然有同样的问题。
let f : int -> a option = fun x -> Some (A x)
let g : a -> b option = fun (A x) -> Some (B x)
let opt_bind : ('a -> 'b option) -> 'a option -> 'b option =
Fun.flip Option.bind
let result : b option =
let x : a option = opt_bind f (Some 42) in
let x : b option = opt_bind g x in
x ;;
但
let result =
let x = Option.bind (Some 42) f in
let x = Option.bind x g in
x
工作正常。
为什么'opt_bind'对'g'有错误的类型期望,好像'opt_bind'不是通用的?
如何使用带有“|>”符号的“绑定”?