Types 为什么可以';t行多态性是否可用于在异构容器中存储对象?

Types 为什么可以';t行多态性是否可用于在异构容器中存储对象?,types,polymorphism,ocaml,Types,Polymorphism,Ocaml,,行多态性不能用于创建异构容器 特别是,行多态性不能用于在同一容器中放置不同类型的对象。例如,不能使用行多态性创建异构元素的列表 举例如下: type square = < area : float; width : int >;; type shape = < variant : repr; area : float> and circle = < variant : repr; area : float; radius : int > and line =

,行多态性不能用于创建异构容器

特别是,行多态性不能用于在同一容器中放置不同类型的对象。例如,不能使用行多态性创建异构元素的列表

举例如下:

type square = < area : float; width : int >;;
type shape = < variant : repr; area : float>
and circle = < variant : repr; area : float; radius : int >
and line = < variant : repr; area : float; length : int >
and repr =
 | Circle of circle
 | Line of line;;
# let hlist: < area: float; ..> list = [square 10; circle 30] ;;
    Characters 49-58:
    Error: This expression has type < area : float; radius : int >
           but an expression was expected of type < area : float; width : int >
           The second object type has no method radius
type square=;;
类型形状=<变量:repr;区域:浮动>
和圆=<变量:repr;面积:浮子;半径:int>
和line=<变量:repr;面积:浮子;长度:int>
和报告=
|圆中之圆
|一行一行;;
#让hlist:列表=[正方形10;圆圈30];;
字符49-58:
错误:此表达式的类型为
但预期表达类型为
第二种对象类型没有方法半径
正如错误消息明确指出的,元素的类型不匹配。我的问题是,为什么行多态性不能“隐藏”不匹配记录的方法,即对容器中的所有类型进行交集


如果您按照逻辑结论的思路进行思考,那么您将得到一个类型错误较少的系统,但许多推断类型没有方法。

这是可以做到的,但您必须明确地这样做:

let hlist = [(square 10 :> shape); (circle 30 :> shape)]
它没有自动完成的原因可能是,否则类型系统将变得不可判定(根据它已经处于不可判定的边缘)。但我不知道细节