Skip to content

Commit 0ae6fd5

Browse files
authored
document nested record definitions (#1026)
1 parent 341de45 commit 0ae6fd5

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

pages/docs/manual/v12.0.0/record.mdx

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,88 @@ type person = {
2929

3030
</CodeTab>
3131

32+
You can also nest definitions of records.
33+
34+
<CodeTab labels={["ReScript", "JS Output"]}>
35+
36+
```res prelude
37+
type person = {
38+
age: int,
39+
name: string,
40+
notificationSettings: {
41+
sendEmails: bool,
42+
allowPasswordLogin: bool,
43+
},
44+
}
45+
46+
let person = {
47+
age: 90,
48+
name: "Test Person",
49+
notificationSettings: {
50+
sendEmails: true,
51+
allowPasswordLogin: false,
52+
},
53+
}
54+
55+
```
56+
```js
57+
let person = {
58+
age: 90,
59+
name: "Test Person",
60+
notificationSettings: {
61+
sendEmails: true,
62+
allowPasswordLogin: false
63+
}
64+
};
65+
```
66+
67+
</CodeTab>
68+
69+
Nesting record definitions is a nice way to group records that are part of the same structure, and won't be referenced from the outside.
70+
71+
If you end up needing to refer to a nested record type explicitly, you should make it an explicit definition instead of a nested one. This is mainly for 2 reasons:
72+
- The records that are automatically generated for the nested record definitions are named in a way that would require you to use escaped identifiers to reference them. The nested record at `notificationSettings` above would be named `\"person.notificationSettings"` for instance
73+
- For the sake of clarity (and caring about your co-workers), having an explicit and named definition to look at and refer to is much easier than scanning a potentially large record definition for the nested record you're looking for
74+
75+
So if we in the example above ended up needing to refer to `person.notificationSettings` nested record from the outside, we should instead make it explicit, just like how we normally define records:
76+
77+
<CodeTab labels={["ReScript", "JS Output"]}>
78+
79+
```res prelude
80+
type personNotificationSettings = {
81+
sendEmails: bool,
82+
allowPasswordLogin: bool,
83+
}
84+
85+
type person = {
86+
age: int,
87+
name: string,
88+
notificationSettings: personNotificationSettings
89+
}
90+
91+
let person = {
92+
age: 90,
93+
name: "Test Person",
94+
notificationSettings: {
95+
sendEmails: true,
96+
allowPasswordLogin: false,
97+
},
98+
}
99+
100+
```
101+
```js
102+
let person = {
103+
age: 90,
104+
name: "Test Person",
105+
notificationSettings: {
106+
sendEmails: true,
107+
allowPasswordLogin: false
108+
}
109+
};
110+
```
111+
112+
</CodeTab>
113+
32114
## Creation
33115

34116
To create a `person` record (declared above):

0 commit comments

Comments
 (0)