user3599828
user3599828

Reputation: 331

How to use specify data types in curried function?

I'm trying to make a curried function in SMLNJ that appends a (string * bool) pair to a list. I could do this:

fun push L a b = (a,b) :: L;
-val push = fn : ('a * 'b) list -> 'a -> 'b -> ('a * 'b) list

That works, but I want a function that only accepts a (string * bool) list, string, and bool. I can't figure out how to write the function signature.

Upvotes: 1

Views: 86

Answers (1)

John Coleman
John Coleman

Reputation: 51998

It is enough to add an explicit type annotation in the function definition:

fun push L a b = (a:string,b:bool) :: L;

The inferred type is

val push = fn : (string * bool) list -> string -> bool -> (string * bool) list

Having said that -- I am not a big fan of needlessly making polymorphic functions less polymorphic. Rigid type checking doesn't need to be on the level of utility functions.

Upvotes: 3

Related Questions