为什么某些OCaml函数将()作为参数? [英] Why do some OCaml functions take () as a parameter?

查看:69
本文介绍了为什么某些OCaml函数将()作为参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Unix模块中的示例:

Example in Unix module:

val environment : unit -> string array

为什么不只是:

val environment : string array

?

推荐答案

因为它表示一个将单位类型的值作为参数的函数.单位类型仅由值()"占用.通常用来表示该功能将执行某种IO或产生副作用,并且不需要任何输入.您提供的第二种类型签名是值的签名,而不是可以应用的函数.如果将某个表达式绑定到该名称,则该表达式将在值绑定发生时(而不是在引用它时)(在函数应用程序中)进行评估.

Because it denotes a function that takes a value of type unit as its parameter. The unit type is only inhabited by the value "()". This is usually used to mean that the function is going to perform some kind of IO or induce a side-effect, and needs no input. The second type signature you provided is the signature for a value, not a function that can be applied. If some expression were bound to this name, that expression would be evaluated at the time the value binding takes place, not at the time it is referenced (as is the case with function application).

这篇关于为什么某些OCaml函数将()作为参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆