rescript-compiler
rescript-compiler copied to clipboard
Expression to represent an empty record with optional fields.
There is currently no way to represent an empty record with optional fields.
The V4 PPX gets around this by adding key
to every record, so the empty record is represented as {key: ?None}
.
It would be nice to lift this restriction so empty records are first class somehow.
For example, one could try to use type coercion:
type allOptional = {x?: int, y?:string}
let none = (() :> allOptional)
See discussion in https://github.com/rescript-lang/rescript-compiler/pull/5619/files#r959143392
Why not just
let none: allOptional = {}
?
I know we may want to have {}
for empty function bodies. But maybe both would be possible? In TS, we can have
type AllOptional = { x?: number; y?: string };
const none: AllOptional = {};
// Function with empty body
const f = () => {};
// Function returning empty object of type AllOptional
const g = (): AllOptional => ({});
There's lots of ambiguity with parsing {}
.
I just remember that it is tricky territory, and one would need to re-familiarise with the parser details.
Certainly, I was just curious if this is a direction we would want to go in (if technically feasible in the parser).
Relevant: https://github.com/rescript-lang/syntax/pull/299 On one side, there's competition for that syntax real estate. On the other side, it shows it's possible to use it, and repurpose it if more compelling.
Separately, there's the semantic question. I would expect some assert false if the existing type checker gets an empty record -- something to test.