Skip to content
This repository has been archived by the owner on Nov 13, 2023. It is now read-only.

Enums Christmas edition: support one object type, plus strings/booleans/integers. #118

Merged
merged 8 commits into from
Dec 27, 2018
Merged
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,18 @@ A file can export many components by defining them in sub-modules. The toplevel

### enums

Enums are Reason polymorphic variants without payload: essentially flat sequences of identifiers. E.g. type `` [ | `monday | `tuesday ] ``.
Enums are Reason polymorphic variants without payload: essentially flat sequences of identifiers. E.g. type ``[@genType] type days = [ | `monday | `tuesday ] ``.
The corresponding JS representation is `"monday"`, `"tuesday"`.

The `@genType.as` annotation can be used to change the name of an element on the JS side of things. So e.g. `` [ | [@genType.as "type"] `type_ ] `` exports Reason value `` `type_ `` to JS value `"type"`.
The `@genType.as` annotation can be used to change the name of an element on the JS side of things. So e.g. ``` [ | [@genType.as "type"] `type_ ] ``` exports Reason value `` `type_ `` to JS value `"type"`.
Boolean/integer/float constants can be expressed as ``` | [@genType.as true] `True ``` and ``` | [@genType.as 20] `Twenty ``` and ``` | [@genType.as 0.5] `Half ```.

See for example [Enums.re](examples/typescript-react-example/src/Enums.re).
At most one variant can have a payload, which must have object type, e.g.
``` [ | `unnamed | `named({. "name": string, "surname": string}) ] ```. Object types are arrays, objects, records and tuples.

See for example [Enums.re](examples/typescript-react-example/src/Enums.re) and [EnumsWithPayload.re](examples/typescript-react-example/src/EnumsWithPayload.re).

**NOTE** When exporting/importing values that use enum types, you have to use type annotations for enums, and cannot rely on type inference. So instead of ```let monday = `monday```, use ```let monday : days = `monday```. The former does not work, as the type checker infers a tyoe without annotations.

### imported types

Expand Down
40 changes: 20 additions & 20 deletions examples/flow-react-example/src/Enums.gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@
* @nolint
*/

const $$toRE175361521 = {"type": 449540197, "module": -134553037, "XXX THIS IS DIFFERENT": 23437694};
const $$toJS1061900109 = {"120": "x", "26810": "same"};

const $$toJS18951405 = {"449540197": "type", "-134553037": "module", "23437694": "42"};
const $$toRE694113598 = {"saturday": -29784519, "sunday": 569248848};

const $$toRE396727132 = {"monday": -949852400, "tuesday": 323181965, "wednesday": -863289194, "thursday": 122883354, "friday": 835226847, "saturday": -29784519, "sunday": 569248848};
const $$toRE1061900109 = {"x": 120, "same": 26810};

const $$toJS98879741 = {"120": "x", "26810": "same"};
const $$toJS584768163 = {"449540197": "type", "-134553037": "module", "23437694": "XXX THIS IS DIFFERENT"};

const $$toRE98879741 = {"x": 120, "same": 26810};
const $$toJS930788378 = {"120": "x", "26809": "same"};

const $$toRE18951405 = {"type": 449540197, "module": -134553037, "42": 23437694};
const $$toJS508922110 = {"449540197": "type", "-134553037": "module", "23437694": "42"};

const $$toJS149274715 = {"120": "x", "26809": "same"};
const $$toRE930788378 = {"x": 120, "same": 26809};

const $$toRE916593523 = {"saturday": -29784519, "sunday": 569248848};
const $$toRE508922110 = {"type": 449540197, "module": -134553037, "42": 23437694};

const $$toJS916593523 = {"-29784519": "saturday", "569248848": "sunday"};
const $$toRE584768163 = {"type": 449540197, "module": -134553037, "XXX THIS IS DIFFERENT": 23437694};

const $$toRE149274715 = {"x": 120, "same": 26809};
const $$toJS694113598 = {"-29784519": "saturday", "569248848": "sunday"};

const $$toJS175361521 = {"449540197": "type", "-134553037": "module", "23437694": "XXX THIS IS DIFFERENT"};
const $$toRE288839514 = {"monday": -949852400, "tuesday": 323181965, "wednesday": -863289194, "thursday": 122883354, "friday": 835226847, "saturday": -29784519, "sunday": 569248848};

// $FlowExpectedError: Reason checked type sufficiently
import * as EnumsBS from './Enums.bs';
Expand All @@ -41,7 +41,7 @@ export type x1 = "x" | "same";

export type x2 = "x" | "same";

export const isWeekend: (weekday) => boolean = function _(Arg1) { const result = EnumsBS.isWeekend($$toRE396727132[Arg1]); return result };
export const isWeekend: (weekday) => boolean = function _(Arg1) { const result = EnumsBS.isWeekend($$toRE288839514[Arg1]); return result };

export const monday: "monday" = "monday";

Expand All @@ -51,20 +51,20 @@ export const sunday: "sunday" = "sunday";

export const onlySunday: ("sunday") => void = function _(Arg1) { const result = EnumsBS.onlySunday(/* sunday */569248848); return result };

export const swap: ("saturday" | "sunday") => "saturday" | "sunday" = function _(Arg1) { const result = EnumsBS.swap($$toRE916593523[Arg1]); return $$toJS916593523[result] };
export const swap: ("saturday" | "sunday") => "saturday" | "sunday" = function _(Arg1) { const result = EnumsBS.swap($$toRE694113598[Arg1]); return $$toJS694113598[result] };

export const testConvert: (testGenTypeAs) => testGenTypeAs = function _(Arg1) { const result = EnumsBS.testConvert($$toRE18951405[Arg1]); return $$toJS18951405[result] };
export const testConvert: (testGenTypeAs) => testGenTypeAs = function _(Arg1) { const result = EnumsBS.testConvert($$toRE508922110[Arg1]); return $$toJS508922110[result] };

export const fortytwoOK: testGenTypeAs = $$toJS18951405[EnumsBS.fortytwoOK];
export const fortytwoOK: testGenTypeAs = $$toJS508922110[EnumsBS.fortytwoOK];

export const fortytwoBAD: "fortytwo" = "fortytwo";

export const testConvert2: (testGenTypeAs2) => testGenTypeAs2 = function _(Arg1) { const result = EnumsBS.testConvert2($$toRE18951405[Arg1]); return $$toJS18951405[result] };
export const testConvert2: (testGenTypeAs2) => testGenTypeAs2 = function _(Arg1) { const result = EnumsBS.testConvert2($$toRE508922110[Arg1]); return $$toJS508922110[result] };

export const testConvert3: (testGenTypeAs3) => testGenTypeAs3 = function _(Arg1) { const result = EnumsBS.testConvert3($$toRE175361521[Arg1]); return $$toJS175361521[result] };
export const testConvert3: (testGenTypeAs3) => testGenTypeAs3 = function _(Arg1) { const result = EnumsBS.testConvert3($$toRE584768163[Arg1]); return $$toJS584768163[result] };

export const testConvert2to3: (testGenTypeAs2) => testGenTypeAs3 = function _(Arg1) { const result = EnumsBS.testConvert2to3($$toRE18951405[Arg1]); return $$toJS175361521[result] };
export const testConvert2to3: (testGenTypeAs2) => testGenTypeAs3 = function _(Arg1) { const result = EnumsBS.testConvert2to3($$toRE508922110[Arg1]); return $$toJS584768163[result] };

export const id1: (x1) => x1 = function _(Arg1) { const result = EnumsBS.id1($$toRE149274715[Arg1]); return $$toJS149274715[result] };
export const id1: (x1) => x1 = function _(Arg1) { const result = EnumsBS.id1($$toRE930788378[Arg1]); return $$toJS930788378[result] };

export const id2: (x2) => x2 = function _(Arg1) { const result = EnumsBS.id2($$toRE98879741[Arg1]); return $$toJS98879741[result] };
export const id2: (x2) => x2 = function _(Arg1) { const result = EnumsBS.id2($$toRE1061900109[Arg1]); return $$toJS1061900109[result] };
42 changes: 42 additions & 0 deletions examples/flow-react-example/src/EnumsWithPayload.bs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions examples/flow-react-example/src/EnumsWithPayload.gen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @flow strict
* @generated
* @nolint
*/

const $$toJS542320962 = {"97": "a", "98": "bRenamed", "937218926": true, "-574635695": 20, "803296723": 0.5};

const $$toRE542320962 = {"a": 97, "bRenamed": 98, "true": 937218926, "20": -574635695, "0.5": 803296723};

// $FlowExpectedError: Reason checked type sufficiently
import * as EnumsWithPayloadBS from './EnumsWithPayload.bs';

export type payload = {|+x: number, +y?: string|};

export type withPayload = "a" | "bRenamed" | true | 20 | 0.5 | payload;

export const testWithPayload: (withPayload) => withPayload = function _(Arg1) { const result = EnumsWithPayloadBS.testWithPayload((typeof(Arg1) === 'object' ? [/* c */99, [Arg1.x, Arg1.y]] : $$toRE542320962[Arg1.toString()])); return (typeof(result) === 'object' ? {x:result[1][0], y:result[1][1]} : $$toJS542320962[result]) };

export const printEnumValue: (withPayload) => void = function _(Arg1) { const result = EnumsWithPayloadBS.printEnumValue((typeof(Arg1) === 'object' ? [/* c */99, [Arg1.x, Arg1.y]] : $$toRE542320962[Arg1.toString()])); return result };
27 changes: 27 additions & 0 deletions examples/flow-react-example/src/EnumsWithPayload.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
type payload = {
x: int,
y: option(string),
};

type withPayload = [
| `a
| [@genType.as "bRenamed"] `b
| [@genType.as true] `True
| [@genType.as 20] `Twenty
| [@genType.as 0.5] `Half
| `c(payload)
];

[@genType]
let testWithPayload = (x: withPayload) => x;

[@genType]
let printEnumValue = (x: withPayload) =>
switch (x) {
| `a => Js.log("printEnumValue: a")
| `b => Js.log("printEnumValue: b")
| `True => Js.log("printEnumValue: True")
| `Twenty => Js.log("printEnumValue: Twenty")
| `Half => Js.log("printEnumValue: Half")
| `c(payload) => Js.log4("printEnumValue x:", payload.x, "y:", payload.y)
};
40 changes: 20 additions & 20 deletions examples/typescript-react-example/src/Enums.gen.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
/* TypeScript file generated by genType. */

const $$toRE175361521 = {"type": 449540197, "module": -134553037, "XXX THIS IS DIFFERENT": 23437694};
const $$toJS1061900109 = {"120": "x", "26810": "same"};

const $$toJS18951405 = {"449540197": "type", "-134553037": "module", "23437694": "42"};
const $$toRE694113598 = {"saturday": -29784519, "sunday": 569248848};

const $$toRE396727132 = {"monday": -949852400, "tuesday": 323181965, "wednesday": -863289194, "thursday": 122883354, "friday": 835226847, "saturday": -29784519, "sunday": 569248848};
const $$toRE1061900109 = {"x": 120, "same": 26810};

const $$toJS98879741 = {"120": "x", "26810": "same"};
const $$toJS584768163 = {"449540197": "type", "-134553037": "module", "23437694": "XXX THIS IS DIFFERENT"};

const $$toRE98879741 = {"x": 120, "same": 26810};
const $$toJS930788378 = {"120": "x", "26809": "same"};

const $$toRE18951405 = {"type": 449540197, "module": -134553037, "42": 23437694};
const $$toJS508922110 = {"449540197": "type", "-134553037": "module", "23437694": "42"};

const $$toJS149274715 = {"120": "x", "26809": "same"};
const $$toRE930788378 = {"x": 120, "same": 26809};

const $$toRE916593523 = {"saturday": -29784519, "sunday": 569248848};
const $$toRE508922110 = {"type": 449540197, "module": -134553037, "42": 23437694};

const $$toJS916593523 = {"-29784519": "saturday", "569248848": "sunday"};
const $$toRE584768163 = {"type": 449540197, "module": -134553037, "XXX THIS IS DIFFERENT": 23437694};

const $$toRE149274715 = {"x": 120, "same": 26809};
const $$toJS694113598 = {"-29784519": "saturday", "569248848": "sunday"};

const $$toJS175361521 = {"449540197": "type", "-134553037": "module", "23437694": "XXX THIS IS DIFFERENT"};
const $$toRE288839514 = {"monday": -949852400, "tuesday": 323181965, "wednesday": -863289194, "thursday": 122883354, "friday": 835226847, "saturday": -29784519, "sunday": 569248848};

// tslint:disable-next-line:no-var-requires
const EnumsBS = require('./Enums.bs');
Expand All @@ -43,7 +43,7 @@ export type x1 = "x" | "same";
// tslint:disable-next-line:interface-over-type-literal
export type x2 = "x" | "same";

export const isWeekend: (_1:weekday) => boolean = function _(Arg1: any) { const result = EnumsBS.isWeekend($$toRE396727132[Arg1]); return result };
export const isWeekend: (_1:weekday) => boolean = function _(Arg1: any) { const result = EnumsBS.isWeekend($$toRE288839514[Arg1]); return result };

export const monday: "monday" = "monday";

Expand All @@ -53,20 +53,20 @@ export const sunday: "sunday" = "sunday";

export const onlySunday: (_1:"sunday") => void = function _(Arg1: any) { const result = EnumsBS.onlySunday(/* sunday */569248848); return result };

export const swap: (_1:"saturday" | "sunday") => "saturday" | "sunday" = function _(Arg1: any) { const result = EnumsBS.swap($$toRE916593523[Arg1]); return $$toJS916593523[result] };
export const swap: (_1:"saturday" | "sunday") => "saturday" | "sunday" = function _(Arg1: any) { const result = EnumsBS.swap($$toRE694113598[Arg1]); return $$toJS694113598[result] };

export const testConvert: (_1:testGenTypeAs) => testGenTypeAs = function _(Arg1: any) { const result = EnumsBS.testConvert($$toRE18951405[Arg1]); return $$toJS18951405[result] };
export const testConvert: (_1:testGenTypeAs) => testGenTypeAs = function _(Arg1: any) { const result = EnumsBS.testConvert($$toRE508922110[Arg1]); return $$toJS508922110[result] };

export const fortytwoOK: testGenTypeAs = $$toJS18951405[EnumsBS.fortytwoOK];
export const fortytwoOK: testGenTypeAs = $$toJS508922110[EnumsBS.fortytwoOK];

export const fortytwoBAD: "fortytwo" = "fortytwo";

export const testConvert2: (_1:testGenTypeAs2) => testGenTypeAs2 = function _(Arg1: any) { const result = EnumsBS.testConvert2($$toRE18951405[Arg1]); return $$toJS18951405[result] };
export const testConvert2: (_1:testGenTypeAs2) => testGenTypeAs2 = function _(Arg1: any) { const result = EnumsBS.testConvert2($$toRE508922110[Arg1]); return $$toJS508922110[result] };

export const testConvert3: (_1:testGenTypeAs3) => testGenTypeAs3 = function _(Arg1: any) { const result = EnumsBS.testConvert3($$toRE175361521[Arg1]); return $$toJS175361521[result] };
export const testConvert3: (_1:testGenTypeAs3) => testGenTypeAs3 = function _(Arg1: any) { const result = EnumsBS.testConvert3($$toRE584768163[Arg1]); return $$toJS584768163[result] };

export const testConvert2to3: (_1:testGenTypeAs2) => testGenTypeAs3 = function _(Arg1: any) { const result = EnumsBS.testConvert2to3($$toRE18951405[Arg1]); return $$toJS175361521[result] };
export const testConvert2to3: (_1:testGenTypeAs2) => testGenTypeAs3 = function _(Arg1: any) { const result = EnumsBS.testConvert2to3($$toRE508922110[Arg1]); return $$toJS584768163[result] };

export const id1: (_1:x1) => x1 = function _(Arg1: any) { const result = EnumsBS.id1($$toRE149274715[Arg1]); return $$toJS149274715[result] };
export const id1: (_1:x1) => x1 = function _(Arg1: any) { const result = EnumsBS.id1($$toRE930788378[Arg1]); return $$toJS930788378[result] };

export const id2: (_1:x2) => x2 = function _(Arg1: any) { const result = EnumsBS.id2($$toRE98879741[Arg1]); return $$toJS98879741[result] };
export const id2: (_1:x2) => x2 = function _(Arg1: any) { const result = EnumsBS.id2($$toRE1061900109[Arg1]); return $$toJS1061900109[result] };
42 changes: 42 additions & 0 deletions examples/typescript-react-example/src/EnumsWithPayload.bs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions examples/typescript-react-example/src/EnumsWithPayload.gen.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* TypeScript file generated by genType. */

const $$toJS542320962 = {"97": "a", "98": "bRenamed", "937218926": true, "-574635695": 20, "803296723": 0.5};

const $$toRE542320962 = {"a": 97, "bRenamed": 98, "true": 937218926, "20": -574635695, "0.5": 803296723};

// tslint:disable-next-line:no-var-requires
const EnumsWithPayloadBS = require('./EnumsWithPayload.bs');

// tslint:disable-next-line:interface-over-type-literal
export type payload = {readonly x: number, readonly y?: string};

// tslint:disable-next-line:interface-over-type-literal
export type withPayload = "a" | "bRenamed" | true | 20 | 0.5 | payload;

export const testWithPayload: (_1:withPayload) => withPayload = function _(Arg1: any) { const result = EnumsWithPayloadBS.testWithPayload((typeof(Arg1) === 'object' ? [/* c */99, [Arg1.x, Arg1.y]] : $$toRE542320962[Arg1.toString()])); return (typeof(result) === 'object' ? {x:result[1][0], y:result[1][1]} : $$toJS542320962[result]) };

export const printEnumValue: (_1:withPayload) => void = function _(Arg1: any) { const result = EnumsWithPayloadBS.printEnumValue((typeof(Arg1) === 'object' ? [/* c */99, [Arg1.x, Arg1.y]] : $$toRE542320962[Arg1.toString()])); return result };
27 changes: 27 additions & 0 deletions examples/typescript-react-example/src/EnumsWithPayload.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
type payload = {
x: int,
y: option(string),
};

type withPayload = [
| `a
| [@genType.as "bRenamed"] `b
| [@genType.as true] `True
| [@genType.as 20] `Twenty
| [@genType.as 0.5] `Half
| `c(payload)
];

[@genType]
let testWithPayload = (x: withPayload) => x;

[@genType]
let printEnumValue = (x: withPayload) =>
switch (x) {
| `a => Js.log("printEnumValue: a")
| `b => Js.log("printEnumValue: b")
| `True => Js.log("printEnumValue: True")
| `Twenty => Js.log("printEnumValue: Twenty")
| `Half => Js.log("printEnumValue: Half")
| `c(payload) => Js.log4("printEnumValue x:", payload.x, "y:", payload.y)
};
4 changes: 2 additions & 2 deletions examples/typescript-react-example/src/ImportJsValue.gen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {area as areaNotChecked} from './MyMath';

import {useColor as useColorNotChecked} from './MyMath';

const $$toJS479407683 = {"322339018": "tomato", "-999567389": "gray"};
const $$toJS580645844 = {"322339018": "tomato", "-999567389": "gray"};

// In case of type error, check the type of 'round' in 'ImportJsValue.re' and './MyMath'.
export const roundTypeChecked: (_1:number) => number = roundNotChecked;
Expand All @@ -24,7 +24,7 @@ export const area: unknown = function _(Arg1: any) { const result = areaTypeChec
export const useColorTypeChecked: (_1:color) => number = useColorNotChecked;

// Export 'useColor' early to allow circular import from the '.bs.js' file.
export const useColor: unknown = function _(Arg1: any) { const result = useColorTypeChecked($$toJS479407683[Arg1]); return result } as (_1:color) => number;
export const useColor: unknown = function _(Arg1: any) { const result = useColorTypeChecked($$toJS580645844[Arg1]); return result } as (_1:color) => number;

// tslint:disable-next-line:no-var-requires
const ImportJsValueBS = require('./ImportJsValue.bs');
Expand Down
Loading