给定以下类型:
type ('props,'state) reactInstance =
{
props: 'props;
state: 'state;
updater:
'event .
(('props,'state) reactInstance -> 'event -> 'state) ->
('props,'state) reactInstance -> 'event -> unit;}
我正在努力实现:
let rec updater f instance event =
let nextState = f instance event in
let newInstance =
{ props; state = nextState; updater } in
()
let newInstance =
{ props; state = (reactClass.getInitialState ()); updater }
我给了更新程序一个类似 forall 的类型定义。我的主要动机是因为更新程序将被事件调用。事先不知道该事件会是什么。它可以是用户界面上的点击或按键等。
内部发生的问题updater
定义于{ props; state = nextState; **updater** }
:
Error: This field value has type
(React.reactInstance props#1618 state#1200 => 'a => state#1200) =>
React.reactInstance props#1618 state#1200 => 'a => unit
which is less general than
'event.
(React.reactInstance 'props 'state => 'event => 'state) =>
React.reactInstance 'props 'state => 'event => unit
为什么会在内部发生这种情况let rec updater...
on updater
而不是在定义记录时updater
in let newInstance
?
我该如何解决这个问题?
你正在做所谓的“多态递归”。这是一个递归函数,可以在每个递归循环中对不同类型调用。就您而言,它的类型没有太大不同,但将函数放入带有 forall 的容器中。
众所周知,多态递归是无法确定的推断,因此您需要通过使用来帮助类型检查器多态注释 https://ocaml.org/manual/polymorphism.html。在这种情况下,还需要eta扩展实例函数(参见ivg的其他答案)。这是最终结果。请注意,您的函数缺少参数。
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
()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)