在下面的程序中,我们知道valStr.value假设泛型类型t的子类型t。然而,当我在poly中检查它时,类型显示为t。在poly解释器中,我能看到t专门化为pair吗?
这就是我运行poly时得到的结果:
> poly
Poly/ML 5.5.2 Release
> use "forum.ml";
signature PAIR =
sig val coord : pair val getFirst : pair -> real type pair end
structure Pair :
sig
val coord : pair
val getFirst : pair -> real
type pair = real * real
end
signature VALUE = sig type t val value : t end
functor createVal (R : PAIR) : VALUE
val extracted = 1.0: real
val main = fn: unit -> unit
structure valStr : VALUE
val it = (): unit
> valStr.value;
val it = (1.0, 2.0): valStr.t
(* I want to see that it is of type "pair" *)用来产生它的代码是:
(* forum.ml *)
signature PAIR =
sig
type pair
val coord : pair
val getFirst : pair -> real
end
structure Pair =
struct
type pair = real * real
val coord = ((1.0,2.0) : pair)
fun getFirst ((x,y) : pair):real = x
end
signature VALUE =
sig
type t
val value: t
end
functor createVal(R : PAIR) : VALUE =
struct
type t = R.pair
val value = R.coord
end
structure valStr = createVal(Pair)
val extracted = Pair.getFirst valStr.value
fun main() = print (Real.toString extracted)发布于 2015-08-30 11:36:43
可以添加类型约束:
> valStr.value: Pair.pair;
val it = (1.0, 2.0): Pair.pairPoly试图以一种有用的方式打印该类型,但它无法猜测在任何特定情况下,当存在多个等效类型时,哪种类型最有用。
https://stackoverflow.com/questions/32293838
复制相似问题