首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ReasonML类型与相同类型不匹配

ReasonML类型与相同类型不匹配
EN

Stack Overflow用户
提问于 2019-07-24 01:37:12
回答 1查看 92关注 0票数 4

即使我在ReasonML中使用相同的类型,我也收到类型不匹配的消息。错误是:

代码语言:javascript
运行
复制
[1]   We've found a bug for you!
[1]   /Users/gt/work/real-and-open/frontend/src/domain/classroom/views/RosterLayoutHeader.re 42:10-70
[1]
[1]   40 ┆ <Classroom.Mutation.AddStudent>
[1]   41 ┆   ...{
[1]   42 ┆     (addStudent: (~id: UUID.t, ~classroomId: UUID.t, unit) => unit) =>
[1]         {
[1]   43 ┆       <div>
[1]   44 ┆         <StudentRowHeader
[1]
[1]   This pattern matches values of type
[1]     (~id: UUID.t, ~classroomId: UUID.t, unit) => unit
[1]   but a pattern was expected which matches values of type
[1]     AddStudent.ContainerMutation.mutationFunctionType (defined as
[1]       AddStudent.MutationInternals.mutationFunctionType)

当我用addStudent: AddStudent.MutationInternals.mutationFunctionType替换addStudent: (~id: UUID.t, ~classroomId: UUID.t, unit) => unit

错误变为:

代码语言:javascript
运行
复制
[1]   We've found a bug for you!
[1]   /Users/gt/work/real-and-open/frontend/src/domain/classroom/views/RosterLayoutHeader.re 53:61-70
[1]
[1]   51 ┆ _ =>
[1]   52 ┆   updateClassroom(
[1]   53 ┆     Classroom.Action.ApolloAddStudent(() => addStudent(
[1]   54 ┆       ~id=classroom.local.newStudentId |> Student.Model.getUUIDFromId,
[1]   55 ┆       ~classroomId=classroom.data.id,
[1]
[1]   This expression has type AddStudent.MutationInternals.mutationFunctionType
[1]   It is not a function.

在代码中,AddStudent如下所示:

代码语言:javascript
运行
复制
[@bs.config {jsx: 3}];
module Mutation = [%graphql
  {|
    mutation addStudent($id: ID!, $classroomId: ID!) {
      addStudent(student: {id: $id, classroomId: $classroomId}){
        ...Classroom_Model.Fragment.ClassroomFields
      }
    }
  |}
];

module MutationInternals : ApolloMutation.MutationInternal = {
  type mutationFunctionType = (~id: UUID.t, ~classroomId: UUID.t, unit) => unit;
  let componentName = "AddStudent";

  module Config = Mutation;
  module InternalMutation = ReasonApollo.CreateMutation(Config);

  let callMutationWithApollo = (apolloMutation : ApolloMutation.apolloMutationType(Config.t)) => 
    ((~id: UUID.t, ~classroomId: UUID.t, ()): unit => {
      let newStudent = Config.make(~id, ~classroomId, ());
      apolloMutation(
        ~variables=newStudent##variables,
        // ~refetchQueries=[|"member"|],
        // ~optimisticResponse=Config.t,
        (),
      ) |> ignore;
      () |> ignore;
    }: mutationFunctionType);
};

module ContainerMutation = ApolloMutation.CreateMutationContainer(MutationInternals);

module Jsx2 = ContainerMutation.Jsx2;
let make = ContainerMutation.make;

所以请注意3件事

1)类型:此处引用了type mutationFunctionType = (~id: UUID.t, ~classroomId: UUID.t, unit) => unit;,错误中显示AddStudent.MutationInternals.mutationFunctionType(~id: UUID.t, ~classroomId: UUID.t, unit) => unit不匹配,尽管它们是相同的。

2)当我在调用函数中直接引用该类型时,它告诉我mutationFunctionType不是一个函数,而是一个函数。

3)我正在使用一个函数器来处理我的MutationInternals模块...我想知道这是否会影响类型。

谢谢

EN

Stack Overflow用户

回答已采纳

发布于 2019-07-24 02:06:18

AddStudent.MutationInternals.mutationFunctionType类型是抽象的,编译器不知道它实际上是作为类型的函数实现的

代码语言:javascript
运行
复制
(~id: UUID.t, ~classroomId: UUID.t, unit) => unit

很难猜测,你的情况下问题的根源是什么。你可以通过封装你的模块意外地过度抽象你的代码,或者你试图破坏必要的抽象。在任何情况下,编译器都会阻止您这样做。

通常,这种类型的错误发生在您密封模块时,例如,当您添加模块类型时,例如,

代码语言:javascript
运行
复制
module type S = {type t};
module M : S = { type t = int};

这使得M.t类型变得抽象,因此它不能在期望int的上下文中使用。解决方案是移除这种不必要的密封,例如,

代码语言:javascript
运行
复制
module type S = {type t};
module M = { type t = int};

或者明确地告诉编译器,t不是抽象类型,而是具体的int,例如,

代码语言:javascript
运行
复制
module type S = {type t};
module M: S with type t = int = {
  type t = int;
};

请注意,在您的示例中,我讨论的是这段代码

代码语言:javascript
运行
复制
MutationInternals : ApolloMutation.MutationInternal

它隐藏了mutationFunctionType定义。

票数 4
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57169497

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档