rescript-vscode icon indicating copy to clipboard operation
rescript-vscode copied to clipboard

Module type and intellisense looks weird after functor

Open jmagaram opened this issue 1 year ago • 2 comments

I’m using functors to generate validated primitive types, like a NonNegativeInt. It works. But the tooltip/intellisense for using the functor is kind of strange. For example, in the code below when I try to use my NonNegativeInt method here is what I see…

What I want to see is int instead of C.domain. At this point, what is module C? If I want all traces of C.domain to be replaced with int I think I need to do the BetterNonNegativeInt casting/assignment below, which is an extra step. Is there some way to do all this in a single step? Is this a problem with the VS Code extension?

module type T = {
  type t
  type domain
  let eq: (t, t) => bool
  let make: domain => option<t>
  let value: t => domain
}

module type Configuration = {
  type domain
  let cmp: (domain, domain) => float
  let validate: domain => option<domain>
}

module Make = (C: Configuration): (T with type domain := C.domain) => {
  type t = C.domain
  type domain = C.domain
  let eq = (i: t, j: t) => C.cmp(i, j) === 0.0
  let value = i => i
  let make = (i: domain) => C.validate(i)
}

module NonNegativeInt = Make({
  type domain = int
  let cmp = (x, y) => x < y ? -1.0 : x > y ? 1.0 : 0.0
  let validate = i => i >= 0 ? Some(i) : None
})

module BetterNonNegativeInt: T with type domain := int = NonNegativeInt

jmagaram avatar Mar 01 '23 21:03 jmagaram

Interesting...just tested this out and it is surprising: I would expect the destructive substitution to be reflected in the tooltips and signatures. (For reference, what I would expect to see here would be what I would expect this same program translated to OCaml and using OCaml lsp to show.)

Here are some images for reference...hopefully it helps.


This is the signature for the functor, it matches what I would expect.

00_make_functor


Here is the signature when you mouse hover over NonNegativeInt module. It is the Make functor signature exactly. This is not what I would expect to see.

01_non_neg_int


The type hint as you are typing the function name. I would expect to see int => option<t> here.

02_type_hint


Type hint for the function after it has been physically typed out. It is what I expect to see.

03_mouse_hover


Type hint after hitting the parentheses ...there is C.domain again.

04_tooltip


Here is a "fix" that gives the correct type for the NonNegativeInt signature.

05_fixed_type_hint


With the correct hint:

06_fixed_hint


And with the correct signature:

07_fixed_hover

mooreryan avatar Mar 01 '23 22:03 mooreryan

I hit this a lot too.. Very unfortunate to have to add the typing to the saturated functor ("Module").
I have written these functors intended as a library for our other engineers and requires exactly the knowledge from them I'd like them to be able to avoid.

AlexMoutonNoble avatar Oct 26 '23 16:10 AlexMoutonNoble