Skip to content

Commit 911651d

Browse files
authored
Update README as_result and do notation sections (#155)
1 parent 1e99c05 commit 911651d

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

README.rst

+20-13
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,10 @@ To save memory, both the ``Ok`` and ``Err`` classes are ‘slotted’,
296296
i.e. they define ``__slots__``. This means assigning arbitrary
297297
attributes to instances will raise ``AttributeError``.
298298

299+
300+
``as_result`` Decorator
301+
-----------------------
302+
299303
The ``as_result()`` decorator can be used to quickly turn ‘normal’
300304
functions into ``Result`` returning ones by specifying one or more
301305
exception types:
@@ -340,24 +344,27 @@ unconventional syntax (without the usual ``@``):
340344
print(res.value)
341345

342346

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.
348354

349355
.. sourcecode:: python
350356

351357

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+
)
357363

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.
361368

362369
This is similar to Rust's `m! macro <https://docs.rs/do-notation/latest/do_notation/>`_:
363370

0 commit comments

Comments
 (0)