-
Notifications
You must be signed in to change notification settings - Fork 44
Support for tuples #71
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
open Belt; | ||
|
||
[@genType] | ||
let testTuple = ((a, b)) => a + b; | ||
|
||
[@genType] | ||
type coord = (int, int, option(int)); | ||
|
||
[@genType] | ||
let origin = (0, 0, Some(0)); | ||
|
||
[@genType] | ||
let computeArea = ((x, y, z)) => | ||
Option.(x * y * z->(mapWithDefault(1, n => n))); | ||
|
||
[@genType] | ||
let computeAreaWithIdent = ((x, y, z): coord) => | ||
Option.(x * y * z->(mapWithDefault(1, n => n))); | ||
|
||
[@genType] | ||
let computeAreaNoConverters = ((x: int, y: int)) => x * y; | ||
|
||
[@genType] | ||
let coord2d = (x, y) => (x, y, None); | ||
|
||
[@genType] | ||
type coord2 = (int, int, Js.Nullable.t(int)); | ||
|
||
[@genType] | ||
type person = { | ||
name: string, | ||
age: int, | ||
}; | ||
|
||
[@genType] | ||
type couple = (person, person); | ||
|
||
[@genType] | ||
let getFirstName = ((first, _second): couple) => first.name; | ||
|
||
[@genType] | ||
let marry = (first, second): couple => (first, second); | ||
|
||
[@genType] | ||
let changeSecondAge = ((first, second): couple): couple => ( | ||
first, | ||
{...second, age: second.age + 1}, | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* @flow strict | ||
* @generated | ||
* @nolint | ||
*/ | ||
|
||
// $FlowExpectedError: Reason checked type sufficiently | ||
const TuplesBS = require('./Tuples.bs'); | ||
|
||
export const testTuple: ([number, number]) => number = TuplesBS.testTuple; | ||
|
||
export type coord = [number, number, ?number]; | ||
|
||
export const origin: [number, number, ?number] = TuplesBS.origin; | ||
|
||
export const computeArea: ([number, number, ?number]) => number = function _(Arg1) { const result = TuplesBS.computeArea([Arg1[0], Arg1[1], (Arg1[2] == null ? undefined : Arg1[2])]); return result }; | ||
|
||
export const computeAreaWithIdent: (coord) => number = function _(Arg1) { const result = TuplesBS.computeAreaWithIdent([Arg1[0], Arg1[1], (Arg1[2] == null ? undefined : Arg1[2])]); return result }; | ||
|
||
export const computeAreaNoConverters: ([number, number]) => number = TuplesBS.computeAreaNoConverters; | ||
|
||
export const coord2d: <T1,T2,T3>(T1, T2) => [T1, T2, ?T3] = TuplesBS.coord2d; | ||
|
||
export type coord2 = [number, number, ?number]; | ||
|
||
export type person = {|+name: string, +age: number|}; | ||
|
||
export type couple = [person, person]; | ||
|
||
export const getFirstName: (couple) => string = function _(Arg1) { const result = TuplesBS.getFirstName([[Arg1[0].name, Arg1[0].age], [Arg1[1].name, Arg1[1].age]]); return result }; | ||
|
||
export const marry: (person, person) => couple = function _(Arg1, Arg2) { const result = TuplesBS.marry([Arg1.name, Arg1.age], [Arg2.name, Arg2.age]); return [{name:result[0][0], age:result[0][1]}, {name:result[1][0], age:result[1][1]}] }; | ||
|
||
export const changeSecondAge: (couple) => couple = function _(Arg1) { const result = TuplesBS.changeSecondAge([[Arg1[0].name, Arg1[0].age], [Arg1[1].name, Arg1[1].age]]); return [{name:result[0][0], age:result[0][1]}, {name:result[1][0], age:result[1][1]}] }; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
open Belt; | ||
|
||
[@genType] | ||
let testTuple = ((a, b)) => a + b; | ||
|
||
[@genType] | ||
type coord = (int, int, option(int)); | ||
|
||
[@genType] | ||
let origin = (0, 0, Some(0)); | ||
|
||
[@genType] | ||
let computeArea = ((x, y, z)) => | ||
Option.(x * y * z->(mapWithDefault(1, n => n))); | ||
|
||
[@genType] | ||
let computeAreaWithIdent = ((x, y, z): coord) => | ||
Option.(x * y * z->(mapWithDefault(1, n => n))); | ||
|
||
[@genType] | ||
let computeAreaNoConverters = ((x: int, y: int)) => x * y; | ||
|
||
[@genType] | ||
let coord2d = (x, y) => (x, y, None); | ||
|
||
[@genType] | ||
type coord2 = (int, int, Js.Nullable.t(int)); | ||
|
||
[@genType] | ||
type person = { | ||
name: string, | ||
age: int, | ||
}; | ||
|
||
/* [@genType] | ||
type couple = (person, person); | ||
|
||
[@genType] | ||
let getFirstName = ((first, _second): couple) => first.name; | ||
|
||
[@genType] | ||
let marry = (first, second): couple => (first, second); | ||
|
||
[@genType] | ||
let changeSecondAge = ((first, second): couple): couple => ( | ||
first, | ||
{...second, age: second.age + 1}, | ||
); */ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* Typescript file generated by genType. */ | ||
|
||
// tslint:disable-next-line:no-var-requires | ||
const TuplesBS = require('./Tuples.bs'); | ||
|
||
export const testTuple: (_1:[number, number]) => number = TuplesBS.testTuple; | ||
|
||
// tslint:disable-next-line:interface-over-type-literal | ||
export type coord = [number, number, (null | undefined | number)]; | ||
|
||
export const origin: [number, number, (null | undefined | number)] = TuplesBS.origin; | ||
|
||
export const computeArea: (_1:[number, number, (null | undefined | number)]) => number = function _(Arg1) { const result = TuplesBS.computeArea([Arg1[0], Arg1[1], (Arg1[2] == null ? undefined : Arg1[2])]); return result }; | ||
|
||
export const computeAreaWithIdent: (_1:coord) => number = function _(Arg1) { const result = TuplesBS.computeAreaWithIdent([Arg1[0], Arg1[1], (Arg1[2] == null ? undefined : Arg1[2])]); return result }; | ||
|
||
export const computeAreaNoConverters: (_1:[number, number]) => number = TuplesBS.computeAreaNoConverters; | ||
|
||
export const coord2d: <T1,T2,T3>(_1:T1, _2:T2) => [T1, T2, (null | undefined | T3)] = TuplesBS.coord2d; | ||
|
||
// tslint:disable-next-line:interface-over-type-literal | ||
export type coord2 = [number, number, (null | undefined | number)]; | ||
|
||
// tslint:disable-next-line:interface-over-type-literal | ||
export type person = {readonly name: string, readonly age: number}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ type t = | |
| ObjectC(fieldsC) | ||
| OptionC(t) | ||
| RecordC(fieldsC) | ||
| TupleC(list(t)) | ||
and groupedArgConverter = | ||
| ArgConverter(label, t) | ||
| GroupConverter(list((string, t))) | ||
|
@@ -79,6 +80,8 @@ let rec toString = converter => | |
++ "}"; | ||
|
||
| OptionC(c) => "option(" ++ toString(c) ++ ")" | ||
| TupleC(innerTypesC) => | ||
"[" ++ (innerTypesC |> List.map(toString) |> String.concat(", ")) ++ "]" | ||
}; | ||
|
||
let typToConverter = (~language, ~exportTypeMap, ~typesFromOtherFiles, typ) => { | ||
|
@@ -157,6 +160,7 @@ let typToConverter = (~language, ~exportTypeMap, ~typesFromOtherFiles, typ) => { | |
), | ||
) | ||
|
||
| Tuple(innerTypes) => TupleC(innerTypes |> List.map(visit(~visited))) | ||
| TypeVar(_) => IdentC | ||
} | ||
and typToGroupedArgConverter = (~visited, typ) => | ||
|
@@ -222,6 +226,8 @@ let rec converterIsIdentity = (~toJS, converter) => | |
} | ||
|
||
| RecordC(_) => false | ||
| TupleC(innerTypesC) => | ||
innerTypesC |> List.for_all(converterIsIdentity(~toJS)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mostly the same as the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Option has no special role here, while it does for objects. So this is perfect. |
||
}; | ||
|
||
let rec apply = (~converter, ~enumTables, ~toJS, value) => | ||
|
@@ -419,6 +425,20 @@ let rec apply = (~converter, ~enumTables, ~toJS, value) => | |
|> String.concat(", "); | ||
"[" ++ fieldValues ++ "]"; | ||
}; | ||
| TupleC(innerTypesC) => | ||
"[" | ||
++ ( | ||
innerTypesC | ||
|> List.mapi((i, c) => | ||
value | ||
++ "[" | ||
++ string_of_int(i) | ||
++ "]" | ||
|> apply(~converter=c, ~enumTables, ~toJS) | ||
) | ||
|> String.concat(", ") | ||
) | ||
++ "]" | ||
}; | ||
|
||
let toJS = (~converter, ~enumTables, value) => | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -403,6 +403,18 @@ and translateTypeExpr_ = | |
[], | ||
[], | ||
) | ||
| Ttuple(listExp) => | ||
let innerTypesTranslation = | ||
listExp |> translateTypeExprs_(~language, ~typeVarsGen, ~typeEnv); | ||
let innerTypes = innerTypesTranslation |> List.map(({typ, _}) => typ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very inspired on the |
||
let innerTypesDeps = | ||
innerTypesTranslation | ||
|> List.map(({dependencies, _}) => dependencies) | ||
|> List.concat; | ||
|
||
let tupleType = Tuple(innerTypes); | ||
|
||
{dependencies: innerTypesDeps, typ: tupleType}; | ||
|
||
| Tlink(t) => | ||
t | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I add Tuples.re also to the TypeScript example folder?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See below.