我最近注意到在函数声明中允许在函数名之前使用类型变量。但我看不到它是如何使用的。以下是一些使用它的示例:
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
因此,基于此,我有几个问题。首先,什么是函数名之前的类型变量用例的一个很好的例子(与参数或返回类型签名中更常见的类型变量相反)。另外,有没有办法表明我想专注于类型,就像我可以使用类型一样?:
> type 'a t = 'a list;
eqtype 'a t
> type f = string t;
type f = string t
我确实通过创建一个val f
具有显式类型签名的新变量 来声明特化,但我认为这不是一回事。例如,来自上面的类型示例,我希望能够做到这一点:
> val s = string print;
Error-Value or constructor (string) has not been declared Found near string print
Static Errors
但这失败了。
最后,为什么类型变量在函数内部隐藏了参数的类型?我只是猜测会发生这种情况,因为 PolyML.print 函数会打印一个问号(表示它不知道类型)而不是实际值。即使我声明了f
明确约束类型的新函数,它仍然不知道传递的变量的类型。(虽然我很确定这个特定部分与函数上的初始类型变量无关,而是与参数上的(隐式)类型变量无关a
。)