gpt4 book ai didi

types - ReasonML 类型与相同类型不匹配

转载 作者:行者123 更新时间:2023-12-01 00:12:03 25 4
gpt4 key购买 nike

即使我在 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) => unitaddStudent: 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定义。

关于types - ReasonML 类型与相同类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57169497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com