How to combat type shadowing?

I’ve got 2 records from 2 modules that look exactly the same. This is causing havoc in unit tests and other places because the compiler is all like “Dude, you’re returning the wrong type” when it fact it’s clearly dilineated between merchantInfov2 and merchantInfov3. Unlike Elm, ReScript is like “Dude, those records are the same” and I’m all like “No, bruh, they aren’t”.

I tried the module trick in my unit tests to stuff the stubs in there, but compiler is still mad. Any tactics beyond “add a random property you’ll never use to the record, lelz” to fix?

can you show an example of the issue you’re facing?

What you’re describing seems a bit odd given Rescript records are nominally typed (unlike Elm if I’m not mistaken) and not structurally typed, so it should distinguish between the two without any issues.

For example:

module Foo = {
  type t = {baz: string}
}

module Bar = {
  type t = {baz: string}
}

let foo = {Foo.baz: "hello"}

let bar = {Bar.baz: "hello"}

// this won't compile because they're not the same type
Js.log2("are they the same ?", foo == bar)
1 Like

What you showed, yes, that’s almost what I’m doing. I could never get the Foo.baz and Bar.baz prefixes to work. Compiler never likes it like you and Yawar told me in the past. Lemme try again…

Yeah, when I do it, I get this weird compiler error:

...
ProductTypesV2.merchantStoreInfo.publicId:"756a1256-0e18-4ced-9828-a33955c4426d",
ProductTypesV2.merchantStoreInfo.groupDealerId:Some(3197),
...

Error:

  108 ┆ ProductTypesV2.merchantStoreInfo.dealerId:601,
  109 ┆ ProductTypesV2.merchantStoreInfo.publicId:"756a1256-0e18-4ced-9828-a33
      ┆ 955c4426d",
  110 ┆ ProductTypesV2.merchantStoreInfo.groupDealerId:Some(3197),
  111 ┆ ProductTypesV2.merchantStoreInfo.companyDealerId:600,
  112 ┆ ProductTypesV2.merchantStoreInfo.status:ProductTypesV2.Inactive,

  I'm not sure what to parse here when looking at ",".

Like… what…

Oh wait, this works:

ProductTypesV2.publicId:"756a1256-0e18-4ced-9828-a33955c4426d",
ProductTypesV2.groupDealerId:Some(3197),

What an odd syntax; it just magically knows the “field is in that module somewhere amongst the type jungle Jesse created”?

That’s why it’s recommended to have types in separated modules, canonically called t, this way you can easily namespace your types.

2 Likes

I totally missed the “t” part, and NOW I get it. I’ve been using Promise.t and Js.Array.t all over the place, but it never clicked to do that myself, doh! Thanks!!!

2 Likes

Everything[1] in ReScript is namespaced by modules, even record fields and variant data constructors :slight_smile:

[1] Well, except structurally typed entities like polymorphic variant data.

1 Like