Skip to content

Commit 7b1e32b

Browse files
committed
fix lint
1 parent c91e18c commit 7b1e32b

2 files changed

Lines changed: 32 additions & 30 deletions

File tree

drafts/records.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ properties and equality checks using a function-like instantiation syntax.
6868
Records can implement interfaces and use traits but cannot extend other records or classes;
6969
composition is allowed, however.
7070

71-
#### Syntax and semantics
71+
### Syntax and semantics
7272

73-
##### Definition
73+
#### Definition
7474

7575
A `record` is defined by the word "record", followed by the name of its type, an open parenthesis containing one or more
7676
typed parameters that become public, immutable, properties.
@@ -114,12 +114,12 @@ record PaintBucket(StockPaint ...$constituents) {
114114
}
115115
```
116116

117-
##### Usage
117+
#### Usage
118118

119119
A `record` may be used as a `readonly class`,
120120
as the behavior of it is very similar with no key differences to assist in migration from `readonly class`.
121121

122-
##### Optional parameters and default values
122+
#### Optional parameters and default values
123123

124124
A `record` can also be defined with optional parameters that are set if left out during instantiation.
125125

@@ -128,7 +128,7 @@ record Rectangle(int $x, int $y = 10);
128128
var_dump(Rectangle(10)); // output a record with x: 10 and y: 10
129129
```
130130

131-
##### Auto-generated `with` method
131+
#### Auto-generated `with` method
132132

133133
To enhance the usability of records, the RFC proposes automatically generating a `with` method for each record.
134134
This method allows for partial updates of properties, creating a new instance of the record with the specified
@@ -163,7 +163,7 @@ $pluto = $pluto->with(population: 1);
163163
$mickey = $pluto->with(name: "Mickey"); // no named argument for population error
164164
```
165165

166-
##### Constructors
166+
#### Constructors
167167

168168
Optionally, they may also define a constructor to provide validation or other initialization logic:
169169

@@ -185,7 +185,7 @@ record User(string $name, string $email) {
185185
During construction, a `record` is fully mutable.
186186
This allows the developer freedom to mutate properties as needed to ensure a canonical representation of an object.
187187

188-
#### Performance considerations
188+
### Performance considerations
189189

190190
To ensure that records are both performant and memory-efficient,
191191
the RFC proposes leveraging PHP's copy-on-write (COW) semantics (similar to arrays) and interning values.
@@ -200,7 +200,7 @@ $point3 = Point(3, 4); // No data duplication here either, it is pointing the th
200200
$point4 = $point1->with(x: 5); // Data duplication occurs here, creating a new instance with modified data
201201
```
202202

203-
##### Cloning and with()
203+
#### Cloning and with()
204204

205205
Calling `clone` on a `record` results in the exact same record object being returned. As it is a "value" object, it
206206
represents a value and is the same thing as saying `clone 3`—you expect to get back a `3`.
@@ -210,7 +210,7 @@ This is an important consideration because a developer may call `$new = $record-
210210
crash.
211211
If a developer wants to crash, they can do by `assert($new !== $record)`.
212212

213-
#### Equality
213+
### Equality
214214

215215
A `record` is always strongly equal (`===`) to another record with the same value in the properties,
216216
much like an `array` is strongly equal to another array containing the same elements.
@@ -270,16 +270,18 @@ foreach ($constructor->getParameters() as $param) {
270270
- A new function, `is_record($record)`, will return `true` for records, and `false` otherwise
271271
- Calling `get_class($record)` will return the record name
272272

273-
#### var_dump
273+
### var_dump
274274

275275
Calling `var_dump` will look much like it does for objects, but instead of `object` it will say `record`.
276276

277-
record(Point)#1 (2) {
278-
["x"]=>
279-
int(1)
280-
["y"]=>
281-
int(2)
282-
}
277+
```txt
278+
record(Point)#1 (2) {
279+
["x"]=>
280+
int(1)
281+
["y"]=>
282+
int(2)
283+
}
284+
```
283285

284286
### Considerations for implementations
285287

published/records.ptxt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ $uid = $user->id;
2626
// ...
2727
$uid = 5; // somehow accidentally sets uid to an unrelated integer
2828
// ...
29-
updateUserRole($uid, Role::ADMIN()); // accidental passing of
29+
updateUserRole($uid, Role::ADMIN()); // accidental passing of
3030
</code>
3131

3232
In this example, the uid is accidentally set to a plain integer, and updateUserRole is called with the wrong value.
@@ -58,9 +58,9 @@ updateUserRole($uid, Role::ADMIN()); // This will throw an error
5858

5959
This RFC proposes the introduction of a new record keyword in PHP to define immutable data objects. These objects will allow properties to be initialized concisely and will provide built-in methods for common operations such as modifying properties and equality checks using a function-like instantiation syntax. Records can implement interfaces and use traits but cannot extend other records or classes; composition is allowed, however.
6060

61-
=== Syntax and semantics ===
61+
==== Syntax and semantics ====
6262

63-
== Definition ==
63+
=== Definition ===
6464

6565
A ''%%record%%'' is defined by the word "record", followed by the name of its type, an open parenthesis containing one or more typed parameters that become public, immutable, properties. They may optionally implement an interface using the ''%%implements%%'' keyword. A ''%%record%%'' body is optional.
6666

@@ -89,18 +89,18 @@ record PaintBucket(StockPaint ...$constituents) {
8989
public function mixIn(StockPaint $paint): PaintBucket {
9090
return $this->with(...$this->constituents, $paint);
9191
}
92-
92+
9393
public function color(): Pigment {
9494
return array_reduce($this->constituents, fn($color, $paint) => $color->mix($paint->color, $paint->volume), Pigment(0, 0, 0));
9595
}
9696
}
9797
</code>
9898

99-
== Usage ==
99+
=== Usage ===
100100

101101
A ''%%record%%'' may be used as a ''%%readonly class%%'', as the behavior of it is very similar with no key differences to assist in migration from ''%%readonly class%%''.
102102

103-
== Optional parameters and default values ==
103+
=== Optional parameters and default values ===
104104

105105
A ''%%record%%'' can also be defined with optional parameters that are set if left out during instantiation.
106106

@@ -109,7 +109,7 @@ record Rectangle(int $x, int $y = 10);
109109
var_dump(Rectangle(10)); // output a record with x: 10 and y: 10
110110
</code>
111111

112-
== Auto-generated with method ==
112+
=== Auto-generated with method ===
113113

114114
To enhance the usability of records, the RFC proposes automatically generating a ''%%with%%'' method for each record. This method allows for partial updates of properties, creating a new instance of the record with the specified properties updated.
115115

@@ -139,7 +139,7 @@ $pluto = $pluto->with(population: 1);
139139
$mickey = $pluto->with(name: "Mickey"); // no named argument for population error
140140
</code>
141141

142-
== Constructors ==
142+
=== Constructors ===
143143

144144
Optionally, they may also define a constructor to provide validation or other initialization logic:
145145

@@ -151,7 +151,7 @@ record User(string $name, string $email) {
151151
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
152152
throw new InvalidArgumentException("Invalid email address");
153153
}
154-
154+
155155
$this->id = hash('sha256', $email);
156156
$this->name = ucwords($name);
157157
}
@@ -160,7 +160,7 @@ record User(string $name, string $email) {
160160

161161
During construction, a ''%%record%%'' is fully mutable. This allows the developer freedom to mutate properties as needed to ensure a canonical representation of an object.
162162

163-
=== Performance considerations ===
163+
==== Performance considerations ====
164164

165165
To ensure that records are both performant and memory-efficient, the RFC proposes leveraging PHP's copy-on-write (COW) semantics (similar to arrays) and interning values. Unlike interned strings, the garbage collector will be allowed to clean up these interned records when they are no longer needed.
166166

@@ -172,13 +172,13 @@ $point3 = Point(3, 4); // No data duplication here either, it is pointing the th
172172
$point4 = $point1->with(x: 5); // Data duplication occurs here, creating a new instance with modified data
173173
</code>
174174

175-
== Cloning and with() ==
175+
=== Cloning and with() ===
176176

177177
Calling ''%%clone%%'' on a ''%%record%%'' results in the exact same record object being returned. As it is a "value" object, it represents a value and is the same thing as saying ''%%clone 3%%''—you expect to get back a ''%%3%%''.
178178

179179
''%%with%%'' may be called with no arguments, and it is the same behavior as ''%%clone%%''. This is an important consideration because a developer may call ''%%$new = $record->with(...$array)%%'' and we don’t want to crash. If a developer wants to crash, they can do by ''%%assert($new !== $record)%%''.
180180

181-
=== Equality ===
181+
==== Equality ====
182182

183183
A ''%%record%%'' is always strongly equal (''%%===%%'') to another record with the same value in the properties, much like an ''%%array%%'' is strongly equal to another array containing the same elements. For all intents, ''%%$recordA === $recordB%%'' is the same as ''%%$recordA == $recordB%%''.
184184

@@ -232,11 +232,11 @@ foreach ($constructor->getParameters() as $param) {
232232
* A new function, ''%%is_record($record)%%'', will return ''%%true%%'' for records, and ''%%false%%'' otherwise
233233
* Calling ''%%get_class($record)%%'' will return the record name
234234

235-
=== var_dump ===
235+
==== var_dump ====
236236

237237
Calling ''%%var_dump%%'' will look much like it does for objects, but instead of ''%%object%%'' it will say ''%%record%%''.
238238

239-
<code>
239+
<code txt>
240240
record(Point)#1 (2) {
241241
["x"]=>
242242
int(1)

0 commit comments

Comments
 (0)