作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
给定以下类型:
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...
上updater
而不是用 updater
定义记录时在let newInstance
?我该如何解决这个问题?
最佳答案
您正在执行所谓的“多态递归”。这是一个递归函数,可以在每个递归循环中对不同类型进行调用。在你的例子中,它的类型没有太大不同,但是将函数放入带有 forall 的容器中。
已知多态递归无法确定推断,因此您需要使用 polymorphic annotation 来帮助类型检查器。 。在这种情况下,还需要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
()
关于polymorphism - Ocaml 多态记录类型不太通用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40574396/
我来自 Asp.Net 世界,试图理解 Angular State 的含义。 什么是 Angular 状态?它类似于Asp.Net中的ascx组件吗?是子页面吗?它类似于工作流程状态吗? 我听到很多人
我一直在寻找 3 态拨动开关,但运气不佳。 基本上我需要一个具有以下状态的开关: |开 |不适用 |关 | slider 默认从中间开始,一旦用户向左或向右滑动,就无法回到N/A(未回答)状态。 有人
我是一名优秀的程序员,十分优秀!