I recently noticed that a type variable is allowed before a function name in a function declaration. But I cannot see how it is used. Here are some examples using it:
Poly/ML 5.5.2 Release
> fun 'a print a = PolyML.print (a);
val print = fn: 'a -> 'a
> print "foo";
?
val it = "foo": string
> pint string "foo";
Error-Value or constructor (string) has not been declared
Found near print string "foo"
Static Errors
> string print "foo";
Error-Value or constructor (string) has not been declared
Found near string print "foo"
Static Errors
> val f : string -> string = print;
val f = fn: string -> string
> f "foo";
?
val it = "foo": string
So I have a few questions based on this. First, what is a good example of a use case for the type variable before the function name (as opposed to the more common type variable in an argument or return type signature). Also, is there a way to indicate that I want to specialize on the type like how I can with a type?:
> type 'a t = 'a list;
eqtype 'a t
> type f = string t;
type f = string t
I did declare a specialization by creating a new variable, val f
, with an explicit type signature, but I don't think this is the same thing. For instance, coming from the type example above I'd expect to be able to do this:
> val s = string print;
Error-Value or constructor (string) has not been declared Found near string print
Static Errors
But that fails.
Finally, why did the type variable hide the type of the argument inside the function? I am only guessing this happens at all because the PolyML.print function prints a question mark (indicating it doesn't know the type) rather than the actual value. Even when I declared the new function f
that clearly constrained the type, it still didn't know the type of the variables being passed. (Though I'm pretty sure this particular part has nothing to do with the initial type variable on the function but rather the (implicit) type variable on the argument a
.)