Proposal: warning or error if different scoped variables have the same name.

xuanbao · · 290 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<pre><code>package main import &#34;fmt&#34; func main() { var foo int for i, foo := 0, &#34;Foo&#34;; i &lt; 1; i++ { fmt.Println(i, foo) } fmt.Printf(&#34;%s&#34;, foo) } </code></pre> <p>Here i have the <strong>foo</strong> var that is declared before the other <strong>foo</strong> inside the loop.</p> <p>The behavior is correct due to scope, but i&#39;d like to be warned if i have a variable with the same name (created with the <strong>:=</strong> construct) that the one declared with the <strong>var</strong> keyword before.</p> <p>What do you think about that?</p> <hr/>**评论:**<br/><br/>patrickdlogan: <pre><p>The vet tool can find shadowed variables. It cannot be a compile time error because sometimes shadowing is intentional.</p></pre>wubrgess: <pre><p>Is there a correct, succinct syntax to do what&#39;s expected in the above code snippet?</p></pre>Morgahl: <pre><p>Here:</p> <pre><code>package main import &#34;fmt&#34; func main() { var i int var foo string for i, foo = 0, &#34;Foo&#34;; i &lt; 1; i++ { fmt.Println(i, foo) } fmt.Printf(&#34;%s&#34;, foo) } </code></pre></pre>wubrgess: <pre><p>so there is not, thank you.</p></pre>dlsniper: <pre><p>There are many cases where shadowing is desired (or even needed) so having this powerful tool removed would do good to nobody.</p> <p>Your example is one of many ways to have shadowing done. What if the code would be:</p> <pre><code>for i := 0; i &lt; 1; i++ { foo := someOtherFunction() fmt.Println(i, foo) } </code></pre> <p>foo is declared inside the loop as well now. Should it be flagged or not?</p> <p>Or in this case, where a function is shadowed?</p> <pre><code>package main func demo() { println(&#34;this is a function&#34;) } func main() { demo() demo := &#34;this is not a function anymore&#34; for i, demo := 0, &#34;hello&#34;; i &lt; 10; i++ { println(i, demo) } println(demo) } </code></pre> <p>As <a href="/u/patrickdlogan" rel="nofollow">/u/patrickdlogan</a> pointed out, you can use <code>go vet -shadow</code> or <code>go vet -shadowstrict</code> to see some of the shadowed identifiers.</p> <p>The next version of Gogland IDE will also support shadowing detection, disabled as using a separate algorithm and there&#39;s an ongoing discussion on how to make this better, see: <a href="https://youtrack.jetbrains.com/issue/GO-4403" rel="nofollow">https://youtrack.jetbrains.com/issue/GO-4403</a></p> <p>Depending on the editor you are using there are various ways you can better spot shadowing, see the different color for example in Gogland: <a href="https://i.imgur.com/PCacLxz.png" rel="nofollow">https://i.imgur.com/PCacLxz.png</a></p> <p>I don&#39;t think there&#39;s an algorithm that can detect shadowed identifiers without false positives and that is why this can never be in the compiler. As for warnings, the Go Team said they do not want to have warnings present the compiler so there&#39;s not much that can be done there.</p></pre>8lall0: <pre><p>Thanks for your answer. I completely agree about shadowing (i use it a lot with the <strong>err</strong> checking); my only doubt is that it can create confusion if a shadowed variable has the same name of an upper variable.</p></pre>dlsniper: <pre><blockquote> <p>my only doubt is that it can create confusion if a shadowed variable has the same name of an upper variable.</p> </blockquote> <p>You are contradicting yourself in the same sentence while describing exactly how shadowing works:</p> <blockquote> <p>I completely agree about shadowing (i use it a lot with the err checking)</p> </blockquote> <p>And if you completely agree about shadowing, and you supposedly know all of this already, why would you propose something that you know it&#39;s not good? And have you seen the previous threads here or on golang-nuts about the very same problem? Unless there&#39;s anything new that I&#39;m missing then this is pretty much a rehash of all those previous threads and it will also lead to the same point: nowhere.</p></pre>

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

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