令人困惑的类型声明?
我已经有一段时间没有使用 SML 了,我遇到了这行代码:
type memory = string -> int;
这是否将“内存”定义为一个接受字符串 a 返回 int 的函数,或者完全是其他东西?我搜索过类似的声明,但我似乎找不到一个或弄清楚它的作用。
当我将其放入 SML/NJ 时,我得到的是:
- type memory = string -> int;
type memory = string -> int
I haven't worked with SML in awhile and I came across this line of code:
type memory = string -> int;
Does this define 'memory' to be a function which takes a string a returns an int, or something else entirely? I've searched for a similar declaration but I can't seem to find one or figure out what it does.
When I put it into SML/NJ, I just get this:
- type memory = string -> int;
type memory = string -> int
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
memory
不是一个函数,它只是一个类型的缩写,该类型是一个以字符串作为输入并返回 int 的函数。因此,每当您想编写
string->int
类型的内容时,您都可以编写它的memory
类型。例如,
您可以编写:
这样的
type
声明可以使您的代码更具可读性(例如,不要编写一对x
的类型为int* int
与(x: int*int)
类似,您只需创建一个缩写typepair = int*int
然后您就可以编写x
是pair
类型,就像这样(x:pair)
)。memory
is not a function , it's just an abbreviation for a type that is a function that takes as input a string and returns an int.So whenever you would like to write that something is of type
string->int
you can just write it's of typememory
.For example instead of writing:
you could write:
Such
type
declarations can make your code more readable (e.g. instead of writing that a pairx
is of typeint*int
like(x: int*int)
, you can just create an abbreviationtype pair = int*int
and then you can write thatx
is of typepair
like this(x: pair)
).