@@ -296,6 +296,10 @@ To save memory, both the ``Ok`` and ``Err`` classes are ‘slotted’,
296
296
i.e. they define ``__slots__ ``. This means assigning arbitrary
297
297
attributes to instances will raise ``AttributeError ``.
298
298
299
+
300
+ ``as_result `` Decorator
301
+ -----------------------
302
+
299
303
The ``as_result() `` decorator can be used to quickly turn ‘normal’
300
304
functions into ``Result `` returning ones by specifying one or more
301
305
exception types:
@@ -340,24 +344,27 @@ unconventional syntax (without the usual ``@``):
340
344
print(res.value)
341
345
342
346
343
- Do notation: syntactic sugar for a sequence of ``and_then() `` calls.
344
- Much like the equivalent in Rust or Haskell, but with different syntax.
345
- Instead of ``x <- Ok(1) `` we write ``for x in Ok(1) ``.
346
- Since the syntax is generator-based, the final result must be the first line,
347
- not the last.
347
+ Do notation
348
+ -----------
349
+
350
+ Do notation is syntactic sugar for a sequence of ``and_then() `` calls. Much
351
+ like the equivalent in Rust or Haskell, but with different syntax. Instead of
352
+ ``x <- Ok(1) `` we write ``for x in Ok(1) ``. Since the syntax is
353
+ generator-based, the final result must be the first line, not the last.
348
354
349
355
.. sourcecode :: python
350
356
351
357
352
- >>> final_result: Result[float , int ] = do(
353
- Ok(len(x) + int(y) + 0.5)
354
- for x in Ok("hello")
355
- for y in Ok(True)
356
- )
358
+ final_result: Result[float, int] = do(
359
+ Ok(len(x) + int(y) + 0.5)
360
+ for x in Ok("hello")
361
+ for y in Ok(True)
362
+ )
357
363
358
- NOTE: If you exclude the type annotation e.g. ``Result[float, int] ``
359
- your type checker might be unable to infer the return type.
360
- To avoid an error, you might need to help it with the type hint.
364
+ Note that if you exclude the type annotation, ``final_result: Result[float,
365
+ int] = ... ``, your type checker may be unable to infer the return type. To
366
+ avoid an errors or warnings from your type checker, you should add a type hint
367
+ when using the ``do `` function.
361
368
362
369
This is similar to Rust's `m! macro <https://docs.rs/do-notation/latest/do_notation/ >`_:
363
370
0 commit comments