|
| 1 | +.. _types: |
1 | 2 |
|
2 | 3 | Types |
3 | 4 | ===== |
@@ -132,7 +133,7 @@ if we used a ``type`` alias the user could simply pass a ``String`` to the ``sen |
132 | 133 | The ``case`` construct |
133 | 134 | ---------------------- |
134 | 135 |
|
135 | | -The ``case`` construct together with function application basically comprise everything which you can do with Haskell at runtime. |
| 136 | +The ``case`` construct together with function application basically comprises everything which you can do in Haskell. |
136 | 137 | The ``case`` construct is used to deconstruct a type and gain access to the data contained withtin. |
137 | 138 |
|
138 | 139 |
|
@@ -296,5 +297,68 @@ Some examples for concrete instances of special types: |
296 | 297 | Record syntax |
297 | 298 | ------------- |
298 | 299 |
|
| 300 | +For convenience reasons there is some extra syntax for defining data types which also automatically creates some field accessor functions. |
| 301 | + |
| 302 | +We can write the following: |
| 303 | + |
| 304 | +:: |
| 305 | + |
| 306 | + data TyType = |
| 307 | + Constructor { field1 :: Int |
| 308 | + , field2 :: String |
| 309 | + } |
| 310 | + |
| 311 | +This defines the type the same way as the other ``data`` construct. |
| 312 | +Meaning we can pattern match as usual on the constructor. |
| 313 | + |
| 314 | + |
| 315 | +:: |
| 316 | + |
| 317 | + data MyType = Constructor Int String |
| 318 | + |
| 319 | + let theData = Constructor 9 "hello" :: MyType |
| 320 | + let theInt = case theData of |
| 321 | + Constructor i _ -> i |
| 322 | + |
| 323 | + theInt == 9 |
| 324 | + |
| 325 | +But additionally it also defines two functions ``field1`` and ``field2`` for accessing the fields. |
| 326 | + |
| 327 | +Aka it generates code similar to the following: |
| 328 | + |
| 329 | +:: |
| 330 | + |
| 331 | + data MyType = Constructor Int String |
| 332 | + |
| 333 | + field1 :: MyType -> Int |
| 334 | + field1 (Constructor i _) = i |
| 335 | + |
| 336 | + field2 :: MyType -> String |
| 337 | + field2 (Constructor _ s) = s |
| 338 | + |
| 339 | +Also the two accessor functions ``field1`` and ``field2`` may be used in a special *record update syntax* to create a new record from an old one with altered field contents. |
| 340 | +Additionally the record may be created with a special record creation syntax. |
| 341 | + |
| 342 | +:: |
| 343 | + |
| 344 | + data TyType = |
| 345 | + Constructor { field1 :: Int |
| 346 | + , field2 :: String |
| 347 | + } |
| 348 | + |
| 349 | + let v1 = Constructor 9 "Hello" :: MyType |
| 350 | + |
| 351 | + -- record creation syntax |
| 352 | + let v2 = Constructor { field2 = "World", field1 = 4 } :: MyType |
| 353 | + |
| 354 | + -- update syntax |
| 355 | + let v3 = v2 { field1 = 9 } |
| 356 | + -- updating multiple fields at once |
| 357 | + let v4 = v2 { field1 = 9, field2 "Hello" } |
| 358 | + |
| 359 | + v1 == v4 |
| 360 | + |
| 361 | + -- old records are unchanged |
| 362 | + v2 /= v3 /= v4 |
299 | 363 |
|
300 | 364 |
|
0 commit comments