types - ReasonML 类型与相同类型不匹配
问题描述
即使我在 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: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
定义。
推荐阅读
- c# - .net core 上的数据库图
- r - 如何查找和删除矩阵中的半重复行?
- ruby-on-rails - 显示带有哈希的文件名而不是图像
- swift - 非英语语言的 Swift NSLinguisticTagger 结果
- javascript - classList 和滚动事件
- c# - 当我设置比 1600x900 更大的分辨率时卡住了
- mysql - 无法创建子查询
- javascript - Creating an AJAX JavaScript Button w/ onClick and no jQuery
- python-3.x - 如何使用对对象的内部引用来克隆/深度复制 Python 3.x dict
- sql-server - SQL Server ISNULL 在多表选择中不起作用