Type error with Functors


#1

Hi, I am getting pretty weird type error in this playground

Or here’s the whole snippet

[@bs.config {jsx: 3}];

// I want to produce two or more versions of this component
module GenericComponent = {
  type header = {
    name: string,
    tooltip: option(string),
  };
  type output =
    | Text(string)
    | Metric(string);

  module type Schema = {
    type item;
    let schema: array((header, item => output));
  };

  module Make = (SomeSchema: Schema) => {
    open SomeSchema;
    [@react.component]
    let make = (~data: array(item)) => {
      // the actual implementation is not imporant here, let's say the data is displayed somehow
      <div>
      {"text" |> React.string}
      </div>
    };
  };
}

module Types = {
  type a = {
    key1: string,
    key2: int,
  }
}

module Page1 = {
  module SchemaImpl: GenericComponent.Schema = {
    open GenericComponent;
    type item = Types.a;
    let schema: array((header, item => output)) = [|({name: "col1", tooltip: None}, row => Text(row.key1))|]
  }
  
  module ComponentImpl = GenericComponent.Make(SchemaImpl);
  
  let mockedData: array(Types.a) = [|
    {key1: "val1", key2: 123},
    {key1: "val2", key2: 456},
    |];
   
  [@react.component]
  let make = () => {
    <div>
    {"Sample Text" |> React.string}
    <ComponentImpl data=mockedData />
    </div>
  };
};

ReactDOMRe.renderToElementWithId(<Page1 />, "preview");

What I’m concerned about is this type error that I’m getting

The incompatible parts:
  Types.a
  vs
  SchemaImpl.item

Which (if you take a look at the code), should be same types?
Is there some way to fix it?


#2

Hi, see my answer here: https://forum.rescript-lang.org/t/how-to-fix-values-do-not-match-when-trying-to-use-type-from-module-functor-parameter/2254/4

In general, if you are using BuckleScript or ReScript, that is the best place to get help.


#3

The error is expected: the functor parameter signature declares item abstract, so outside SchemaImpl the compiler only knows SchemaImpl's item, not Types.a. They are not interchangeable. Expose the equality when defining the implementation module, for example by annotating it with a with type item = Types.a constraint on the schema signature. Then the functor sees the same type, and no cast should be needed.