Ocaml polymorphic records type is less general

浪尽此生 提交于 2019-12-06 06:18:53

You are doing what is called "polymorphic recursion". This is a recursive function that could be invoked on different type at each recursion loop. In your case, it's not much different type, but putting the function into a container with a forall.

Polymorphic recursion is known to be undecidable to infer, so you need to help the typechecker a bit by using polymorphic annotation. In this case, you also need to eta expand the instance function (see ivg's other answer). Here is the final result. Note that your function was missing a parameter.

type ('props,'state) reactInstance = {
  props: 'props;
  state: 'state;
  updater:
    'event .
      (('props,'state) reactInstance -> 'event -> 'state) ->
    ('props,'state) reactInstance -> 'event -> unit;}

let rec updater
  : 'event .
    'props ->
    (('props,'state) reactInstance -> 'event -> 'state) ->
    ('props,'state) reactInstance -> 'event -> unit
  = fun props f instance event ->
    let nextUpdater f i e = updater props f i e in
    let nextState = f instance event in
    let newInstance =
      { props; state = nextState; updater = nextUpdater } in
    ()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!