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

Expression to represent an empty record with optional fields.

Open cristianoc opened this issue 1 year ago • 6 comments

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.

cristianoc avatar Aug 31 '22 03:08 cristianoc

For example, one could try to use type coercion:

type allOptional = {x?: int, y?:string}

let none = (() :> allOptional)

cristianoc avatar Aug 31 '22 04:08 cristianoc

See discussion in https://github.com/rescript-lang/rescript-compiler/pull/5619/files#r959143392

cristianoc avatar Aug 31 '22 04:08 cristianoc

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 => ({});

cknitt avatar Aug 31 '22 13:08 cknitt

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.

cristianoc avatar Aug 31 '22 13:08 cristianoc

Certainly, I was just curious if this is a direction we would want to go in (if technically feasible in the parser).

cknitt avatar Aug 31 '22 14:08 cknitt

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.

cristianoc avatar Sep 01 '22 04:09 cristianoc