Can you show an example (preferrably with a playground link) where this happens?
Normally if this happens when destructuring a tuple, the same thing would happen with a direct destructuring of the record. I can’t repro your issue.
module Definition = {
type t = {
a: string,
b: string,
}
let getRecord = () => {a: "", b: ""}
let getList = () => (getRecord(), 42)
}
let doIt = () => {
// compiles
let (x, y) = Definition.getList()
let {a, b} = x
// compiles
let {a, b} = Definition.getRecord()
// breaks
let ({a, b}, y) = Definition.getList()
/*
The record field a can't be found.
If it's defined in another module or file, bring it into scope by:
- Prefixing it with said module name: TheModule.a
- Or specifying its type: let theValue: TheModule.theType = {a: VALUE}
*/
}
to be honest I don’t understand how the first example can even compile:
let doIt = () => {
// compiles
let (x, y) = Definition.getList()
let {a, b} = x
}
given it wouldn’t compile when not inside a function:
let (x, y) = Definition.getList()
let {a, b} = x
About why this happens, well ReScript type system follows Hindley-Milner type inference algorithm, I’m not sure what corner case leads to this phenomenon, but you’re more than welcome to fix it if you can!
Amazing discoveries. And even this does not compile outside of the function. But it does inside:
// breaks
let {a, b} = Definition.getRecord()
let doIt = () => {
// compiles
let {a, b} = Definition.getRecord()
}
This feels even a bigger contradiction to me, and it only involves records, no lists.
That said, I encounter many cases where accessing a record field (or decomposition) fails to compile and needs a type hint. But the type should otherwise be known at that point. I intuitively feel that these should all compile. But I cannot wrap my head around it what exactly is happening here.
I’ll think about this further. Meanwhile, if anyone knows the reason… please do tell!