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

Support for tuples #71

Merged
merged 3 commits into from
Oct 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions examples/reason-react-example/src/basics/Tuples.bs.js

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

48 changes: 48 additions & 0 deletions examples/reason-react-example/src/basics/Tuples.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
open Belt;
Copy link
Contributor Author

@jchavarri jchavarri Oct 21, 2018

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?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See below.


[@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},
);
34 changes: 34 additions & 0 deletions examples/reason-react-example/src/basics/Tuples.re.js
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]}] };
47 changes: 47 additions & 0 deletions examples/typescript-react-example/src/nested/Tuples.bs.js

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

48 changes: 48 additions & 0 deletions examples/typescript-react-example/src/nested/Tuples.re
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},
); */
25 changes: 25 additions & 0 deletions examples/typescript-react-example/src/nested/Tuples.tsx
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};
20 changes: 20 additions & 0 deletions src/Converter.re
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type t =
| ObjectC(fieldsC)
| OptionC(t)
| RecordC(fieldsC)
| TupleC(list(t))
and groupedArgConverter =
| ArgConverter(label, t)
| GroupConverter(list((string, t)))
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -157,6 +160,7 @@ let typToConverter = (~language, ~exportTypeMap, ~typesFromOtherFiles, typ) => {
),
)

| Tuple(innerTypes) => TupleC(innerTypes |> List.map(visit(~visited)))
| TypeVar(_) => IdentC
}
and typToGroupedArgConverter = (~visited, typ) =>
Expand Down Expand Up @@ -222,6 +226,8 @@ let rec converterIsIdentity = (~toJS, converter) =>
}

| RecordC(_) => false
| TupleC(innerTypesC) =>
innerTypesC |> List.for_all(converterIsIdentity(~toJS))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly the same as the ObjectC case above, but didn't use the OptionC switch because I didn't think it was necessary (doesn't that case get process naturally when the recursion continues?)

Copy link
Collaborator

Choose a reason for hiding this comment

The 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) =>
Expand Down Expand Up @@ -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) =>
Expand Down
12 changes: 12 additions & 0 deletions src/Dependencies.re
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,18 @@ and translateTypeExpr_ =
[],
[],
)
| Ttuple(listExp) =>
let innerTypesTranslation =
listExp |> translateTypeExprs_(~language, ~typeVarsGen, ~typeEnv);
let innerTypes = innerTypesTranslation |> List.map(({typ, _}) => typ);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very inspired on the Tconstr(path, typeParams, _) case below.

let innerTypesDeps =
innerTypesTranslation
|> List.map(({dependencies, _}) => dependencies)
|> List.concat;

let tupleType = Tuple(innerTypes);

{dependencies: innerTypesDeps, typ: tupleType};

| Tlink(t) =>
t
Expand Down
9 changes: 8 additions & 1 deletion src/EmitTyp.re
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,14 @@ let rec renderTyp = (~language, ~indent=None, ~inFunType, typ) =>
++ (typ |> renderTyp(~language, ~indent, ~inFunType))
++ ")"
}

| Tuple(innerTypes) =>
"["
++ (
innerTypes
|> List.map(renderTyp(~language, ~indent, ~inFunType))
|> String.concat(", ")
)
++ "]"
| TypeVar(s) => s
}
and renderField =
Expand Down
1 change: 1 addition & 0 deletions src/GenTypeCommon.re
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ type typ =
| Object(fields)
| Option(typ)
| Record(fields)
| Tuple(list(typ))
| TypeVar(string)
and fields = list(field)
and field = {
Expand Down
2 changes: 2 additions & 0 deletions src/Translation.re
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ let abstractTheTypeParameters = (~typeVars, typ) =>
| Object(_)
| Option(_)
| Record(_)
| Tuple(_)
| TypeVar(_) => typ
};

Expand Down Expand Up @@ -498,6 +499,7 @@ let translateTypeDeclaration =
| Object(_) => false
| Option(t) => t |> isOpaque
| Record(_) => false
| Tuple(innerTypes) => innerTypes |> List.exists(isOpaque)
| TypeVar(_) => true
};
let opaque =
Expand Down
Loading