user747302
user747302

Reputation: 21

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 '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

Upvotes: 2

Views: 876

Answers (1)

insumity
insumity

Reputation: 5459

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)).

Upvotes: 8

Related Questions