problem.html 5.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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>Incommensurate E types - 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/advanced/interop.html"><img src="../../../images/prev.png" alt="Prev"></a>
  10. <a accesskey="u" href="../../../tutorial/advanced/interop.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/interop/value-or-error.html"><img src="../../../images/next.png" alt="Next"></a></div><div id="content">
  12. <div class="titlepage"><div><div><h1 style="clear: both">Incommensurate E types</h1></div></div></div>
  13. <p>Back in the essential tutorial section on <code>result</code>, we studied a likely very common
  14. initial choice of <code>E</code> type: <a href="../../../tutorial/essential/result.html">a strongly typed enum</a>.
  15. We saw how <a href="../../../motivation/plug_error_code.html">by marking up strongly typed enums to tell the C++ standard library
  16. what they are</a>, they gain implicit convertibility into <code>std::error_code</code>, and we
  17. then pointed out that you might as well now always set <code>E = std::error_code</code>, as that
  18. comes with the enormous advantage that you can use the boilerplate saving
  19. <code>BOOST_OUTCOME_TRY</code> macro when the <code>E</code> type is always the same.</p>
  20. <p>We thus strongly recommend to users that for any given piece of code, always
  21. using the same <code>E</code> type across the codebase is very wise, except where you explicitly want
  22. to prevent implicit propagation of failure up a call stack e.g. local failures in
  23. some domain specific piece of code.</p>
  24. <p>However it is unreasonable to expect that any non-trivial codebase can make do
  25. with <code>E = std::error_code</code>. This is why Outcome allows you to use <a href="../../payload">custom <code>E</code>
  26. types which carry payload</a> in addition to an error code, yet
  27. still have that custom type treated as if a <code>std::error_code</code>, including <a href="../../payload/copy_file3">lazy custom exception
  28. throw synthesis</a>.</p>
  29. <p>All this is good, but if library A uses <code>result&lt;T, libraryA::failure_info&gt;</code>,
  30. and library B uses <code>result&lt;T, libraryB::error_info&gt;</code> and so on, there becomes
  31. a problem for the application writer who is bringing in these third party
  32. dependencies and tying them together into an application. As a general rule,
  33. each third party library author will not have built in explicit interoperation
  34. support for unknown other third party libraries. The problem therefore lands
  35. with the application writer.</p>
  36. <p>The application writer has one of three choices:</p>
  37. <ol>
  38. <li><p>In the application, the form of result used is <code>result&lt;T, std::variant&lt;E1, E2, ...&gt;&gt;</code>
  39. where <code>E1</code>, <code>E2</code> &hellip; are the failure types for every third party library
  40. in use in the application. This has the advantage of preserving the original
  41. information exactly, but comes with a certain amount of use inconvenience
  42. and maybe excessive coupling between high level layers and implementation detail.</p></li>
  43. <li><p>One can translate/map the third party&rsquo;s failure type into the application&rsquo;s
  44. failure type at the point of the failure
  45. exiting the third party library and entering the application. One might do
  46. this, say, with a C preprocessor macro wrapping every invocation of the third
  47. party API from the application. This approach may lose the original failure detail,
  48. or mis-map under certain circumstances if the mapping between the two systems
  49. is not one-one.</p></li>
  50. <li><p>One can type erase the third party&rsquo;s failure type into some application
  51. failure type, which can later be reconstituted if necessary. This is the cleanest
  52. solution with the least coupling issues and no problems with mis-mapping, but
  53. it almost certainly requires the use of <code>malloc</code>, which the previous two did not.</p></li>
  54. </ol>
  55. <p>Things get even more complicated in the presence of callbacks. If in the
  56. callback you supply to library A, you call library B, you may need to insert
  57. switch statement maps or other mechanisms to convert library B&rsquo;s failures into
  58. something library A can understand, and then somehow extract that out &ndash; preferably
  59. without loss of original information &ndash; into the application&rsquo;s failure handling
  60. mechanism if library A subsequently returns failure as well. This implies
  61. transmitting state by which to track these interrelated pieces of failure data.</p>
  62. <p>Let us see what Outcome can do to help the application writer address some of these
  63. issues, next.</p>
  64. </div><p><small>Last revised: February 09, 2019 at 15:18:26 UTC</small></p>
  65. <hr>
  66. <div class="spirit-nav">
  67. <a accesskey="p" href="../../../tutorial/advanced/interop.html"><img src="../../../images/prev.png" alt="Prev"></a>
  68. <a accesskey="u" href="../../../tutorial/advanced/interop.html"><img src="../../../images/up.png" alt="Up"></a>
  69. <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/advanced/interop/value-or-error.html"><img src="../../../images/next.png" alt="Next"></a></div></body>
  70. </html>