TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Why Const Sucks

30 pointsby jesperhtabout 7 years ago

5 comments

abacateabout 7 years ago
&gt; D&#x27;s const is also transitive, meaning that if something is const, then everything it contains or refers to is also const. If D code has the equivalent of const vector&lt;T*&gt;, then not only is the container treated as const, but the elements are as well.<p>And that is not necessarily what the programmer wants.<p>There are a number of uses for a constant vector and non-const objects. Adding &quot;const&quot; in front of &quot;T&quot; should make everything constant, but just assuming everything is constant does not allow one to have non-const objects.<p>Unless there is a way to override this, it looks like a severe limitation in the language.
评论 #16526343 未加载
signa11about 7 years ago
&gt; All of this means that const actually provides zero guarantees about an object not being mutated (and even in the case where the object is directly constructed as const, that just means that it&#x27;s undefined behavior when the object is mutated, not that the compiler actually protects against it being mutated).<p>if this is the only reason why author thinks that const is a problem, then it is all good. you can do the same thing in C as well. and ofcourse, if you do that, and break things, you get to hold all the pieces as well :)<p>probably a &#x27;constexpr&#x27; semantics is what the author might be looking for ?<p>arne-mertz article on mutable, available here: <a href="https:&#x2F;&#x2F;arne-mertz.de&#x2F;2017&#x2F;10&#x2F;mutable&#x2F;" rel="nofollow">https:&#x2F;&#x2F;arne-mertz.de&#x2F;2017&#x2F;10&#x2F;mutable&#x2F;</a>, which imho, is another side of the const-coin, is quite good as well.<p>edit-001 : slight change.
评论 #16527225 未加载
kazinatorabout 7 years ago
&gt; <i>Now, Java also added a number of nice things that C++ didn&#x27;t have - such as an array knowing its own length, array bounds checking, and other features geared towards protecting the programmer from stupid mistakes.</i><p>C++ has bounds-checked arrays that know their length if you want (hello, <i>std::vector&lt;T&gt;</i>, or roll your own if you don&#x27;t like that one). It just doesn&#x27;t endow the C arrays with it (for good reasons). The C compatibility of C++ means that C++ interoperates easily with C API&#x27;s without the overhead of FFI.
评论 #16532277 未加载
评论 #16527141 未加载
评论 #16527870 未加载
jononorabout 7 years ago
I&#x27;m more annoyed that const is opt-in instead of the default. Though I guess for C++ as a &quot;better C&quot; there wasn&#x27;t much of an option to do that...
gurkendoktorabout 7 years ago
const is one of the reasons why I prefer strongly-typed languages based on reference types. I want to love the no-overhead policy of C++, but value types make const annoying (e.g. no vector&lt;const string&gt;) and require complicated rvalue-reference mechanics to be efficient when working with nested data structures.<p>In Java, where everything is an (expensive) reference type, building an immutable LabeledPoint2D class is a no-brainer. &quot;final String label; final double x, y;&quot; - done. I find this much easier to reason about than the C++ equivalent where the const-ness only emerges from how you use the class.<p>Add Objective-C&#x27;s system where Array&lt;T&gt;, Set&lt;T&gt;, String etc. are all immutable, and only their derived types (MutableArray&lt;T&gt;, MutableSet&lt;T&gt;, MutableString) are mutable, and you get a reasonably effective and efficient implementation of tail-constness without extending your type system.<p>It&#x27;s bizarre how C++ and D both try so hard to implement const, yet higher-level languages that could easily have better const support don&#x27;t really bother (think Java, where you never know which List&lt;T&gt; is mutable).