开发者

Confusing type declaration?

I haven't worked with SML in awhile and I came across this line of code:

type memory = string -> int;

Does this define 'me开发者_如何转开发mory' 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


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 type memory.

For example instead of writing:

- fun foo(f : string->int, s) = f s;
val foo = fn : (string -> int) * string -> int

you could write:

- fun foo( f: memory, s) = f s;
val foo = fn : memory * string -> int

Such type declarations can make your code more readable (e.g. instead of writing that a pair x is of type int*int like (x: int*int), you can just create an abbreviation type pair = int*int and then you can write that x is of type pair like this (x: pair)).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜