作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一段时间没有使用 SML 了,我遇到了这行代码:
type memory = string -> int;
这是否将“内存”定义为一个接受字符串 a 返回 int 的函数,或者完全是其他东西?我搜索过类似的声明,但我似乎无法找到或弄清楚它的作用。
当我把它放入 SML/NJ 时,我得到了这个:
- type memory = string -> int;
type memory = string -> int
最佳答案
memory
不是函数,它只是一种类型的缩写,它是一种函数,它以字符串作为输入并返回一个 int。因此,无论何时你想写一些 string->int
类型的东西,你都可以只写它的 memory
类型。
例如而不是写:
- fun foo(f : string->int, s) = f s;
val foo = fn : (string -> int) * string -> int
你可以这样写:
- fun foo( f: memory, s) = f s;
val foo = fn : memory * string -> int
这样的 type
声明可以使您的代码更具可读性(例如,而不是像 (x: int*int)
,你可以只创建一个缩写 type pair = int*int
然后你可以写 x
是类型对
像这样(x: pair)
).
关于sml - 令人困惑的类型声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5954042/
很抱歉新手的问题,但是: 我最近才发现“=”运算符不只是处理对象/等等。值(value),也是引用。这很酷,但我认为这对变量来说是不一样的,它不会在存储整数或 float 的变量之间创建引用。后来我觉
我是一名优秀的程序员,十分优秀!