conventions.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  3. <title>Conventions - Boost.Outcome documentation</title>
  4. <link rel="stylesheet" href="../../css/boost.css" type="text/css">
  5. <meta name="generator" content="Hugo 0.52 with Boostdoc theme">
  6. <meta name="viewport" content="width=device-width,initial-scale=1.0"/>
  7. <link rel="icon" href="../../images/favicon.ico" type="image/ico"/>
  8. <body><div class="spirit-nav">
  9. <a accesskey="p" href="../../tutorial/essential/no-value/builtin.html"><img src="../../images/prev.png" alt="Prev"></a>
  10. <a accesskey="u" href="../../tutorial/essential.html"><img src="../../images/up.png" alt="Up"></a>
  11. <a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../../tutorial/advanced.html"><img src="../../images/next.png" alt="Next"></a></div><div id="content">
  12. <div class="titlepage"><div><div><h1 style="clear: both">Conventions</h1></div></div></div>
  13. <p>You now know everything you need to get started with using Outcome
  14. immediately.</p>
  15. <p>The initial temptation for most beginners will be to use a bespoke
  16. strongly typed enumeration on a case by case basis i.e. a &ldquo;once off&rdquo;
  17. custom <code>E</code> type. This is usually due to experience in other languages
  18. with sum types e.g. Rust, Haskell, Swift etc.</p>
  19. <p>However this is C++! Not Rust, not Swift, not Haskell! I must caution you to always avoid using
  20. custom <code>E</code> types in public APIs. The reason is that every time
  21. library A using custom <code>E1</code> type must interface with library B
  22. using custom <code>E2</code> type, you must map between those <code>E1</code> and <code>E2</code>
  23. types.</p>
  24. <p>This is information lossy, i.e. fidelity of failure gets lost
  25. after multiple translations. It involves writing, and then
  26. <em>maintaining</em>, a lot of annoying boilerplate. It leaks internal
  27. implementation detail, and fails to separate concerns. And one
  28. cannot use <a href="../../reference/macros/tryv.html" class="api-reference"><code>BOOST_OUTCOME_TRYV(expr)/BOOST_OUTCOME_TRY(expr)</code></a>
  29. if there is no convertibility between <code>E</code> types.</p>
  30. <p>The C++ 11 standard library, and Boost,
  31. specifically ships <code>&lt;system_error&gt;</code> for the purpose of wrapping up
  32. individual custom <code>E</code> types into a generic framework, where disparate
  33. custom <code>E</code> types can discover and interact with one another.
  34. That ships with every C++ compiler.</p>
  35. <p>For all these reasons, this is why <code>result</code> and <code>outcome</code> default
  36. the <code>EC</code> type to error code. You should leave that default alone
  37. where possible.</p>
  38. <hr />
  39. <h3 id="tl-dr">tl;dr;</h3>
  40. <p>Please <a href="../../motivation/plug_error_code.html">plug your library into <code>std::error_code</code></a>,
  41. or <a href="../../experimental.html">equivalent</a>, and do not expose
  42. custom <code>E</code> types in ANY public API. <code>result</code> and <code>outcome</code> default
  43. <code>EC</code> to an error code for good reason.</p>
  44. </div><p><small>Last revised: February 09, 2019 at 15:18:26 UTC</small></p>
  45. <hr>
  46. <div class="spirit-nav">
  47. <a accesskey="p" href="../../tutorial/essential/no-value/builtin.html"><img src="../../images/prev.png" alt="Prev"></a>
  48. <a accesskey="u" href="../../tutorial/essential.html"><img src="../../images/up.png" alt="Up"></a>
  49. <a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../../tutorial/advanced.html"><img src="../../images/next.png" alt="Next"></a></div></body>
  50. </html>