Why don't golang have the constant pointer?

xuanbao · · 1017 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>this caused many null pointer panics in our code.</p> <hr/>**评论:**<br/><br/>TheMerovius: <pre><p>There is some discussion about const pointers (and the like) in <a href="https://groups.google.com/d/msg/golang-nuts/_XiGWtxhmhE/8iWxTYXCfn0J" rel="nofollow">this thread</a>. It&#39;s not clear to me, why the absence of the concept would cause null pointer panics?</p></pre>kl0nos: <pre><p>It&#39;s great to get a panic, you know then that you are dereferencing null pointers in your code. In C/C++ you would be lucky to get an segfault. </p></pre>theOtherOtherBob: <pre><p>Yes, I&#39;m also glad when my face doesn&#39;t get hit when I fall. But you know what&#39;s even better? Not falling in the first place.</p></pre>kl0nos: <pre><p>Then use some access function if you can&#39;t live without it ? It will give you the same functionality. Language creators were pretty clear about this - it will not land in Golang. You have panics = you know where YOU made a mistake, fix it and problem solved. </p></pre>theOtherOtherBob: <pre><blockquote> <p>Then use some access function if you can&#39;t live without it ? It will give you the same functionality.</p> </blockquote> <p>No, it won&#39;t give you a static guarantee like a non-nullable reference would.</p> <blockquote> <p>You have panics = you know where YOU made a mistake</p> </blockquote> <p>No, you don&#39;t. You <em>potentially</em> know where you made a mistake <em>sometime later on</em> when the program is run, which <em>may</em> be during tests <em>if you&#39;re lucky</em> but it also may be days, weeks, months, years later, potentially in production or on someone else&#39;s machine, etc. It is called <a href="https://en.wikipedia.org/wiki/Tony_Hoare#Apologies_and_retractions" rel="nofollow">The billion-dollar mistake</a> for a reason.</p> <p>I prefer to know about my mistake as soon as possible, preferably right now on my machine, preferably before the program even has a chance to run and cause damage.</p> <p>Of course, there&#39;s a never-ending debate on how many static guarantees are enough. Type systems can get arbitrarily complex. However, I don&#39;t think this applies to non-null pointers much, given how incredibly huge the number of problems with nullable pointers &amp; references has been historically (and not just in C/C++ but in memory-safe languges as well) and because non-nullable references aren&#39;t hard to understand or use. Having non-nullable references should be a no-brainer. However, nullable pointers are very deeply rooted in the mainstream culture and heritage of languages like C, Java and others, and so unfortunatelly they feel &#34;natural&#34; to most people even though they are in fact quite silly if you think about it independently of the said heritage.</p> <blockquote> <p>it will not land in Golang</p> </blockquote> <p>I know, Golang is pretty anti-type-systems. Oh well.</p></pre>pdffs: <pre><p>Lack of const pointers didn&#39;t cause panics in your code, poor code caused panics in your code.</p></pre>fix3r: <pre><p>Oh wow, give this man a cigar for the least useful comment in the history of programming.</p></pre>circajerka: <pre><p>Cmon man, it&#39;s common sense - Every single program would be perfect if developers would just write perfect code! /s</p></pre>marksteve4: <pre><p>you can argue this for any language. When it comes to a giant industry level project. The language can offload the mental burden of developers would be the best.</p></pre>hell_0n_wheel: <pre><p>This language does a lot to offload mental burdens. Garbage collection, for one. Functions as a first class object, for another. What exactly is so burdensome about this?</p></pre>: <pre><p>[removed]</p></pre>hell_0n_wheel: <pre><p>If you have a point to make, you can do so without the tone. You&#39;re also encouraged to add to the conversation, as opposed to whatever it is you&#39;re doing here.</p></pre>Macrobian: <pre><p>I&#39;m adding to the discussion by ridiculing the fact that you offered some of the oldest developments of PLT as Go reducing the burden on programmers, in response to an individual being burdened by deficiencies elsewhere in the language. </p> <p>People expect a little more from a language than features available in Scheme 48 years ago. </p></pre>marksteve4: <pre><p>as one example, I need to make sure there is no another goroutine reassign the pointer.</p></pre>hell_0n_wheel: <pre><p>You can&#39;t call golang burdensome, if you&#39;re gonna use it to write bad code.</p> <p>There are simple ways to avoid exactly what you&#39;re talking about, without using <code>const</code>. You could avoid this whole situation altogether, if you&#39;re a little more careful about how you design your code...</p></pre>marksteve4: <pre><p>mind share how to prevent this without using const? that would benefit every go programmer</p></pre>hell_0n_wheel: <pre><p>You want me to debug your code, even though you&#39;re not bothering to share it? That&#39;s almost as ridiculous as modifying a shared pointer in a goroutine. </p></pre>freman: <pre><p>*nil</p></pre>allowthere: <pre><p>Generally, Go doesn&#39;t seem to be fond of constant anything (except for compile-time constants), and the reason is that it&#39;s often hard to reason about constants. For example, </p> <pre><code>final A a = new A(); </code></pre> <p>So you would say &#34;Well, <code>A</code> is a constant. But while the variable A is constant, what A points to isn&#39;t (and if you&#39;re not in Java, you can do something like</p> <pre><code> var final a = NewA(false); var b = &amp;a </code></pre> <p>Would you be able to do</p> <pre><code> (*b) = NewA(true) </code></pre></pre>theOtherOtherBob: <pre><blockquote> <p>and the reason is that it&#39;s often hard to reason about constants</p> </blockquote> <p>Constants aren&#39;t hard to &#39;reason about&#39; when they are actually, you know, constant. That is, when they provide actual immutability guarantees that apply recursively.</p> <p>This is not the case in Java, as it doesn&#39;t really support constness / immutability. Your criticism definitely applies to Java&#39;s <code>final</code> thingy, which is pretty much useless, but not to immutability in general, which should be pretty easy to &#39;reason about&#39;, in fact, the whole point of immutability is to make things easier to &#39;reason about&#39;.</p> <p>Btw. why is &#39;reasoning about things&#39; such a catchphrase these days?</p></pre>tarantulus: <pre><blockquote> <p>why is &#39;reasoning about things&#39; such a catchphrase these days?</p> </blockquote> <p>&#34;I don&#39;t understand this thing, because I&#39;ve not been willing to put the effort in to understand it&#34; == &#34;This is hard to reason about&#34;.</p></pre>ryeguy: <pre><p>I&#39;ve never used generics and I&#39;ve never missed them.</p></pre>theOtherOtherBob: <pre><p>&#34;I find your comment elegant, efficient, and easy to reason about&#34;</p> <p><sup>= &#34;I agree.&#34;</sup></p></pre>irqlnotdispatchlevel: <pre><p>You can not assign the address of a constant to a non-const.</p></pre>RoughMedicine: <pre><p>You picked the worst possible example to talk about constants. Java&#39;s <code>final</code> isn&#39;t even a proper constant anyway, it just means that that reference will always point to the same value.</p> <p>You could have talked about C++&#39;s <code>const</code>. If a variable is <code>const</code>, you can&#39;t take a non-<code>const</code> pointer to it, so your second example wouldn&#39;t work (if you replaced <code>var</code> with <code>auto</code> and <code>final</code> with `const, of course).</p> <p>C++ also gives you <code>const</code>methods, so you can reason about which methods you can call on an <code>const</code> object (because they won&#39;t change it), and which you can&#39;t.</p> <p>So no, constants are not hard to reason about. They&#39;re the opposite. When they are real constants (not Java&#39;s <code>final</code>) they give you a strong guarantee: this object will never change. That&#39;s it. How could that possibly be harder than mutability?</p></pre>theOtherOtherBob: <pre><p>Nit: In C++, it being true to its nature, it&#39;s possible to back-stab <code>const</code> guarantees with <code>mutable</code> or casts. But at least both of those are fairly easy to spot in code...</p></pre>0xjnml: <pre><pre><code>package ref type Int struct{ *int } func NewInt(p *int) Int { return Int{p} } func (i Int) Int() int { return *i.int } </code></pre></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1017 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传