TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

C++14 either monad and why you shouldn't use exceptions

21 点作者 entelechy大约 8 年前

4 条评论

daemin大约 8 年前
This won&#x27;t work well with types that need alignment. It might be better not to have the internal buffer of characters but instead have an anonymous union of both left and right types.<p>Also Expected is not quite the same thing as this. This Maybe and Either is like Expected but with the ability to call .then() on it to do further processing.
评论 #14316800 未加载
entelechy大约 8 年前
alternatives: <a href="https:&#x2F;&#x2F;github.com&#x2F;ptal&#x2F;expected" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ptal&#x2F;expected</a> <a href="https:&#x2F;&#x2F;github.com&#x2F;beark&#x2F;ftl" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;beark&#x2F;ftl</a>
tempodox大约 8 年前
Still looks very clunky and nowhere near the elegance and efficiency of true FP.
mrout大约 8 年前
&gt;Exceptions are 2-3 orders of magnitude slower if exceptions are thrown<p>And free if they aren&#x27;t thrown. It doesn&#x27;t really matter if it&#x27;s slow in the error path, as the error path is.. well.. the error path. You aren&#x27;t doing useful work on the error path anyway.<p>&gt;Exceptions are not part of the type-system<p>False. &#x27;noexcept&#x27; is absolutely part of the type system.<p>&gt;auto unsafe = [] { &#x2F;&#x2F; a function that throws, sometimes we can&#x27;t avoid it...<p>Please don&#x27;t write code like this, this is unidiomatic C++.<p>&gt;Either&lt;std::exception, int&gt; e = Try&lt;std::exception&gt;(unsafe); &#x2F;&#x2F; let&#x27;s lift the exception into the typesystem<p>This is really bad style in Haskell, where you got this idea from, let alone in C++. Either is a symmetric type, it&#x27;s like std::pair&lt;a, b&gt; (in fact it&#x27;s the dual of std::pair). std::either should be to std::variant as std::pair is to std::tuple.<p>(Minor note: Idiomatic C++ is for types to be lowercase, see the std::exception and int.)<p>&gt;e.left() .map([](auto const&amp; e) {<p>These &#x27;functional maps&#x27; are unidiomatic in C++, <i>especially</i> for things that aren&#x27;t ranges or containers.<p>&gt; return std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;<p>I don&#x27;t really understand why you would want to return std::cerr. Why map?<p>&gt;int result = e .leftMap([](auto) { return 42; }) &#x2F;&#x2F; do nothing with exception and map to 42 .rightMap([](auto x) { return x * 2; }) &#x2F;&#x2F; do further computation if value available .join() &#x2F;&#x2F; join both sides of either<p>A different sense of join compared to Haskell as well? Wow that&#x27;s designed to confuse.<p>Instead of doing this, you could just have a couple of `if` statements. There&#x27;s nothing wrong with using `if` statements in C++, honestly. C++ isn&#x27;t Haskell.
评论 #14321549 未加载
评论 #14322626 未加载