How to switch the type of an item in my type signature? [closed]
When using two record types with common field names, one may need to help the typechecker to disambiguate between the two types.
For instance, consider this line:
lecture.priority, workweek.priority
It is impossible to look at this line and deduce that lecture
ought to have type lecture
while workweek
has type workweek
without some more information. And type inference is always local. In such ambiguous situation, the typechecker always pick the last defned types as the default option.
It is possible to avoid this ambiguous choice in a few ways.
- First, we can add an explicit type annotation:
let schedSorter = (lecture:lecture, workweek:workweek) =>
- Another option is to define the two types in their own modules:
module Lecture = {
type t = {
lecture: string,
day: string,
time: list(int),
priority: int,
};
}
module Workweek = {
type t = {
day: string,
time: list(int),
priority: int,
};
};
With this definition, we are now able to distinguish the field Lecture.priority
from the field Workweek.priority
. Thus, the
ambiguous line
lecture.priority, workweek.priority
can be clarified as
lecture.Lecture.priority, workweek.Workweek.priority
- Lastly, in your case, it seems like the
lecture
type contains aworkweek
, thus it might work to rewrite thelecture
type as
type lecture = {
lecture: string,
workweek: workweek
};
which completely avoids the ambiguity of duplicated field names.