Polymorphic function as return value and value restriction in SML

别来无恙 提交于 2019-12-05 13:10:36

For technical reasons, you are not allowed to generalize (i.e., make polymorphic) the results of a function call. The result of a call must have a monomorphic type. If this weren't the case, you could subvert the type system by the following dirty trick:

  1. Call ref [] and get back a list of type forall 'a . 'a list ref
  2. Insert a string.
  3. Remove a function

and there you are: you are now executing the contents of an arbitrary string as code. Not Good.

By insisting that the value returned by ref [] be monomorphic, you ensure that it can be used as a list of strings or a list of functions but not both. So this is part of the price we pay for type safety.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!