|
39 | 39 | FieldN [= ExprN]}). |
40 | 40 | ``` |
41 | 41 |
|
| 42 | +> #### Change {: .info } |
| 43 | +> |
| 44 | +> Since OTP 28.0 the record creation syntax is allowed when defining a record: |
| 45 | +> ```erlang |
| 46 | +> -record #Name{Field1 [= Expr1], |
| 47 | +> ... |
| 48 | +> FieldN [= ExprN]}). |
| 49 | +> ``` |
| 50 | +
|
42 | 51 | The default value for a field is an arbitrary expression, except that it must |
43 | 52 | not use any variables. |
44 | 53 |
|
@@ -230,3 +239,39 @@ record_info(size, Record) -> Size |
230 | 239 |
|
231 | 240 | `Size` is the size of the tuple representation, that is, one more than the |
232 | 241 | number of fields. |
| 242 | + |
| 243 | +## Record name quoting |
| 244 | + |
| 245 | +A record name is an atom. Atoms can be quoted. |
| 246 | +[Reserved words](reference_manual.md#reserved-words) and variable |
| 247 | +names are not atoms, unless quoted. |
| 248 | + |
| 249 | +For example `div` is the integer division operator so it cannot be used |
| 250 | +as a record name unless quoted: |
| 251 | + |
| 252 | +``` erlang |
| 253 | +-record('div', {field :: integer()}). |
| 254 | + |
| 255 | +foo() -> #'div'{field = 17}. |
| 256 | +``` |
| 257 | + |
| 258 | +The same applies to a variable name such as `Var`: |
| 259 | + |
| 260 | +``` erlang |
| 261 | +-record('Var', {field :: integer()}). |
| 262 | + |
| 263 | +foo() -> #'Var'{field = 4711}. |
| 264 | +``` |
| 265 | + |
| 266 | +> #### Change {: .info } |
| 267 | +> |
| 268 | +> Since OTP-28.0, a name after the `#` operator doesn't have to be quoted, |
| 269 | +> and since record definition can be done with record creation syntax, |
| 270 | +> this also works: |
| 271 | +> ``` erlang |
| 272 | +> -record #div{field :: integer()}). |
| 273 | +> -record #Var{field :: integer()}). |
| 274 | +> |
| 275 | +> foo() -> #div{field = 17}. |
| 276 | +> bar() -> #Var{field = 4711}. |
| 277 | +> ``` |
0 commit comments