Code compiles but does not run - GenType issue?

My code assigns unique ids to different types of objects, like PersonId, CompanyId, ProductId, etc. I wrote some code that wraps a uuid package and makes different types (using functors) for each kind of ID so I don’t mix them up and try to join a PersonId with a CompanyId for example. Probably overkill for type safety but that’s what I did. I thought it was working since it compiles just fine but the tests don’t even execute because one of the generated .js files try to import a .gen file that does not exist. I created a very simple project - 4 short .res files - based on the ReScript starter template that illustrates the problem. I’d really appreciate it if a guru could take a quick look and see if I’m doing something wrong, or if this is a bug in GenType or the compiler. You’ll see that UniqueId.bs.js references a non-existent UniqueId.gen. If you take out the @gentype.import statements from UniqueId.res and substitute stubbed functions everything works fine.

I entered a compiler bug about this because I can’t think of a good reason why code that compiles without errors should reference a non-existent .gen file.

1 Like

You can get UniqueId.gen.tsx to generate by changing this

module Make: MakeType = () => {
  @genType.import("./uuid")
  external _validate: string => bool = "validate"

  @genType.import("./uuid")
  external _random: unit => string = "create"

  // snip
}

to this

module External = {
  @genType.import("./uuid")
  external _validate: string => bool = "validate"

  @genType.import("./uuid")
  external _random: unit => string = "create"
}

module Make: MakeType = () => {
  include External

  // snip
}

to let the compiler know those are used values, and thus generate the genType stuff.

Btw, I’m guessing you want the random function also available from TS? (Given that you included it in the module type?)

You will need a genType annotation to make that happen

module type T = {
  include Validated.T
  // Add it right here...
  @genType
  let random: unit => t
}

Note: by the way, I didn’t check if the genType generated files were actually correct or not…just that they were generated…so this exercise could be purely academic!

It seems like you understand when gentype does its thing and when it does not. I will try to get my head around it this morning. I really want to be able to use Functors to generate types I can work with from TypeScript.

1 Like

The change removes the use of functors, as far as @genType annotations are concerned, as an explicit module External is used.
The fact that Make uses that module is orthogonal to GenType, which does not need to be aware of it.

1 Like

I figured out how to get my code to work. I’m a bit dismayed that there are scenarios where the code works fine from ReScript but the generated .gen.tsx files don’t work. For example, I found a case where the return type of a functor must be inlined. I’m not saying anything is broken. I just don’t understand well enough how @gentype and include and functors interact.

1 Like