OCaml中的输出关键字

时间:2018-10-04 19:51:34

标签: ocaml

我想知道关键字output在OCaml中的含义。

我看了看文档:

val output : out_channel -> bytes -> int -> int -> unit

output oc buf pos len writes len characters from byte sequence buf, starting at offset pos, to the given output channel oc. Raise Invalid_argument "output" if pos and len do not designate a valid range of buf.

问题是我完全不理解所有这些含义。

如果您能提供一个使用关键字输出的简单代码示例,那就太好了。

谢谢!

1 个答案:

答案 0 :(得分:2)

没有关键字outputPervasives模块中只有一个名为 function 的输出。

output的目的是将一些字节写入输出通道。

如果您选择可打印的字节,并且您的输出通道是标准输出,则可以通过小测试看到结果:

# let mybytes = Bytes.of_string "hello\n";;
val mybytes : bytes = Bytes.of_string "hello\n"
# output stdout mybytes 0 6;;
hello
- : unit = ()
#

要显示output只是一个标识符(即名称)而不是关键字,请注意,您可以定义自己的名为output的值:

# let output = 3010;;
val output : int = 3010
#

对于诸如then这样的真实关键字,情况并非如此:

# let then = 3010;;
Error: Syntax error
#
相关问题