I would like to define a function as following:
let f (a: int) (b: int) (c: int) (d: int): int =
...
有没有可能在不把它们变成元组的情况下使签名更短?因为我仍然希望
f
有4个参数。非常感谢。 编辑1:我认为重复4次的
int
是无用的,并且想象一下像 let f(a, b, c, d: int): int
这样的东西实际上目前是不允许的。I would like to define a function as following:
let f (a: int) (b: int) (c: int) (d: int): int =
...
f
有4个参数。int
是无用的,并且想象一下像 let f(a, b, c, d: int): int
这样的东西实际上目前是不允许的。尝试使用这个语法:
let g: int -> int -> int -> int -> int =
fun a b c d ->
assert false
虽然不太短,但如果你有很多这样的东西,你可以定义 type arith4 = int -> int -> int -> int -> int
并将该名称用作 g
的类型注释。
我的OCaml有点生疏,但我相信你可以通过声明自己的类型并在函数体中解包来实现。
type four = int*int*int*int
let myfunction (t:four) =
let a, b, c, d = t in
a + b + c + d;
你也可以这样做:
let sum4 ((a, b, c, d):int*int*int*int) =
a + b + c + d;;
type 'a four = int -> int -> int -> int -> 'a;;
- gasche