ReasonML 类型与相同类型不匹配

ReasonML Type mismatch with same type

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

[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: (~id: UUID.t, ~classroomId: UUID.t, unit) => unit 替换为 addStudent: AddStudent.MutationInternals.mutationFunctionType

错误变为:

[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 如下所示:

[@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) 我正在使用 Functor 来处理我的 MutationInternals 模块...我想知道这是否会影响类型。

谢谢

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

类型的函数实现的
(~id: UUID.t, ~classroomId: UUID.t, unit) => unit

很难猜测,您的问题的根源是什么。要么你不小心通过密封你的模块过度抽象了你的代码,要么你试图打破必要的抽象。无论如何,编译器会阻止你这样做。

这种错误一般发生在封装模块时,例如添加模块类型时,例如

module type S = {type t};
module M : S = { type t = int};

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

module type S = {type t};
module M = { type t = int};

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

module type S = {type t};
module M: S with type t = int = {
  type t = int;
};

请注意,在您的情况下,我说的是这段代码

MutationInternals : ApolloMutation.MutationInternal

隐藏了 mutationFunctionType 定义。