Value withFilter不是Cats IO的成员以进行理解

时间:2019-04-16 19:15:10

标签: scala scala-cats

我编写了这段代码,并且可以正常编译

import { render } from "react-dom";
import "react-sortable-tree/style.css";
/* eslint-disable react/no-multi-comp */
import React, { Component } from "react";
import PropTypes from "prop-types";
import { DragDropContext, DragSource } from "react-dnd";
import HTML5Backend from "react-dnd-html5-backend";
import {
  SortableTreeWithoutDndContext as SortableTree,
  getNodeAtPath
} from "react-sortable-tree";

// -------------------------
// Create an drag source component that can be dragged into the tree
// https://react-dnd.github.io/react-dnd/docs-drag-source.html
// -------------------------
// This type must be assigned to the tree via the `dndType` prop as well
const externalNodeType = "yourNodeType";
const externalNodeSpec = {
  // This needs to return an object with a property `node` in it.
  // Object rest spread is recommended to avoid side effects of
  // referencing the same object in different trees.
  beginDrag: componentProps => ({ node: { ...componentProps.node } })
};
const externalNodeCollect = (connect /* , monitor */) => ({
  connectDragSource: connect.dragSource()
  // Add props via react-dnd APIs to enable more visual
  // customization of your component
  // isDragging: monitor.isDragging(),
  // didDrop: monitor.didDrop(),
});
class externalNodeBaseComponent extends Component {
  render() {
    const { connectDragSource, node } = this.props;

    return connectDragSource(
      <div
        style={{
          display: "inline-block",
          padding: "3px 5px",
          background: "blue",
          color: "white"
        }}
      >
        {node.title}
      </div>,
      { dropEffect: "copy" }
    );
  }
}
externalNodeBaseComponent.propTypes = {
  node: PropTypes.shape({ title: PropTypes.string }).isRequired,
  connectDragSource: PropTypes.func.isRequired
};
const YourExternalNodeComponent = DragSource(
  externalNodeType,
  externalNodeSpec,
  externalNodeCollect
)(externalNodeBaseComponent);

class UnwrappedApp extends Component {
  constructor(props) {
    super(props);

    this.state = {
      treeData: [
        {
          title: "Level 1",
          children: [
            {
              title: "Level 2",
              children: [
                {
                  title: "Level 3",
                  children: [
                    { title: "Level 4", children: [{ title: "Level 5" }] }
                  ]
                }
              ]
            }
          ]
        }
      ]
    };
  }

  render() {
    return (
      <div>
        <div style={{ height: 300 }}>
          <SortableTree
            treeData={this.state.treeData}
            onChange={treeData => this.setState({ treeData })}
            dndType={externalNodeType}
            //   canNodeHaveChildren={node => !node.noChildren}
            canDrop={({ nextParent }) => !nextParent || !nextParent.noChildren}
            addAsFirstChild={!this.state.addAsFirstChild}
          />
        </div>
        <YourExternalNodeComponent node={{ title: "Level 1" }} />
        &nbsp;
        <YourExternalNodeComponent node={{ title: "Level 2" }} />
        &nbsp;
        <YourExternalNodeComponent node={{ title: "Level 3" }} />
        &nbsp;
        <YourExternalNodeComponent node={{ title: "Level 4 or 5" }} />
        &nbsp;
      </div>
    );
  }
}

const App = DragDropContext(HTML5Backend)(UnwrappedApp);
export default App;

render(<App />, document.getElementById("root"));

但是如果我将此代码转换为

for {
  list : List[Int] <- Future(List(1, 2, 3))
} yield list.size 

res7: Future[Int] = Future(Success(3))

我收到一个编译时错误

for {
  list : List[Int] <- IO(List(1, 2, 3))
} yield list.size

如果我删除类型,则可以正常编译

value withFilter is not a member of cats.effect.IO[List[Int]]

为什么不能用IO指定类型?

我启用了部分统一,所以不可能是:)

1 个答案:

答案 0 :(得分:6)

您的理解力逐渐形成,它使用功能withFilter,并且由于IO没有该方法,编译会失败。

幸运的是,有一个编译器插件better-monadic-for可以解决这个问题。

只需在您的addCompilerPlugin("com.olegpy" %% "better-monadic-for" % "0.3.0")中添加build.sbt,就可以了。