Can (a==1 && a==2 && a==3) ever evaluate to true?

agolangf · · 507 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<hr/>**评论:**<br/><br/>shovelpost: <pre><p>Sure it can: <a href="https://play.golang.org/p/-gF1qvNiUOy">https://play.golang.org/p/-gF1qvNiUOy</a></p></pre>mitchell-riley: <pre><p>what the fuck</p></pre>epiris: <pre><p>Unicode code points that look similar</p></pre>haskell_leghumper: <pre><p>If you look closely, those aren&#39;t a&#39;s, they&#39;re characters from the Canadian Aboriginal Syllabics block, which are allowed in Go identifiers. From Go&#39;s perspective, those are just three long identifiers. </p></pre>cheesechoker: <pre><p>Not sure <a href="/r/golang" rel="nofollow">/r/golang</a> will ever top that post. Legendary</p></pre>comrade_donkey: <pre><p>Not sure if allusion to generics joke. Have an upvote.</p> <p>Edit: definitely generics joke right here.</p></pre>longtimedockhand: <pre><blockquote> <p>a, ?, а</p> </blockquote> <p><a href="https://www.fileformat.info/info/unicode/char/0061/index.htm" rel="nofollow">U+0061 LATIN SMALL LETTER A</a></p> <p><a href="https://www.fileformat.info/info/unicode/char/1d5ba/index.htm" rel="nofollow">U+1D5BA MATHEMATICAL SANS-SERIF SMALL A</a></p> <p><a href="https://www.fileformat.info/info/unicode/char/0430/index.htm" rel="nofollow">U+0430 CYRILLIC SMALL LETTER A</a></p></pre>mashmorgan: <pre><p>OK, mind blown, can someone explain, so I can goto: sleep</p></pre>epiris: <pre><p>The variables themselves, the value inside the source each has a different unicode code point, i.e.: <a href="https://play.golang.org/p/bo9VnFnpNsW" rel="nofollow">play</a>. Unicode has some characters that look very similar, which has some interesting security consequences. I like to use this fact for unit tests in anything that does byte/string processing, since they are easy to spot the width of the rune and all can jump directly to the upper or lower bound of the a-zA-Z range. So you can write a base set of tests in ascii and then derive multi-width unit tests from them by adding the offsets of rune.</p> <pre><code>const ( // Test unicode points I like to use for utf8, they all are a-z and do not // have a step to alternative casing. Meaning they have the property that: // // (uwN) &#39;A&#39; + 32 = &#39;a&#39; (lower case mapped letter) uw1 = &#39;A&#39; // 0x41 65 (1 byte) uw2 = &#39;À&#39; // 0xC0 192 (2 bytes) uw3 = &#39;A&#39; // 0xFF21 65313 (3 bytes) uw4 = &#39;?&#39; // 0x1D400 119808 (4 bytes) ) </code></pre></pre>mashmorgan: <pre><p>aaaarggggghhhhhhhhh .. of to bed !!!!!!!!!!!!!!!!!!!</p></pre>sacado: <pre><p>Fun but that&#39;s not really answering the main question (it&#39;s not the same &#34;a&#34;).</p></pre>: <pre><p>[deleted]</p></pre>Momer: <pre><p>The shared-state race condition is the only way I could think of in Go as well.</p></pre>theonlycosmonaut: <pre><p>But don&#39;t goroutines only pre-empt each other except at well defined boundaries? Or has that changed? Could there be a data race in between expressions like that?</p> <p>Looks like <a href="/u/nevyn" rel="nofollow">/u/nevyn</a> below has had to add manual yielding in a function call to achieve an approximation.</p></pre>dgryski: <pre><p>A sufficiently smart compiler could easily optimize that comparison to a constant <code>false</code>. Remember that a program with data races is not a valid program.</p></pre>: <pre><p>[deleted]</p></pre>dgryski: <pre><p>gcc is a sufficiently smart compiler, and gccgo has this optimization.</p> <p>Undefined behaviour in C is a real problem because it&#39;s so easy to hit. The Go specification doesn&#39;t even mentioned &#39;undefined&#39;. The Go compiler produces code that assumes there are no data races. I probably should have said &#34;conforming program&#34; instead of &#34;valid program&#34;.</p></pre>jerf: <pre><p>It&#39;s not pointless. This is a legitimate source of bugs in languages with a lot of implicit, overridable behavior, and it&#39;s a valuable data point in the skill level of a programmer whether they know this, because it&#39;s probably because they&#39;ve experienced it.</p></pre>: <pre><p>[deleted]</p></pre>jerf: <pre><p>I assume you&#39;ve never used Ruby then? Check this sort of thing out: <a href="https://www.infoq.com/articles/ruby-open-classes-monkeypatching/">https://www.infoq.com/articles/ruby-open-classes-monkeypatching/</a></p> <p>Yes, it&#39;s obviously stupid in isolation to do this sort of thing, but in the real world, &#34;clever little things&#34; add up. And hoo boy, was Ruby in love with clever little things for a while.</p> <p>I&#39;ve also personally seen bad things happen when I added an override to stringification in perl code. I&#39;ve also seen people do some ill-advised things in Haskell overriding equality specifically, on <a href="/r/Haskell">/r/Haskell</a>.</p> <p>If you&#39;re lucky enough not to have encountered this, well, be thankful for your luck. They can be viciously difficult bugs to track down when they don&#39;t produce crashes, and sometimes even then.</p></pre>: <pre><p>[deleted]</p></pre>epiris: <pre><p>I think the premise for his argument wasn&#39;t well formed, since I agree with you this condition would be a rare encounter. But I do agree with his argument that it isn&#39;t pointless, going through enough thought exercises exactly like this was what allowed you to draw a conclusion. Which enabled you to write two polite and helpful sentences for the OP, the trailing line had a hint of condescension which turned things contentious.</p></pre>sacado: <pre><p>If a candidate can answer OP&#39;s question (in languages where operators can be redefined, not in go), he will probably be able to find potential errors in custom equality operators.</p></pre>nevyn: <pre><p>Eh, wasn&#39;t sure if the compiler would optimize things away (maybe more likely in the original version). <a href="https://play.golang.org/p/QaNx44K_gx4" rel="nofollow">https://play.golang.org/p/QaNx44K_gx4</a></p> <p>Doesn&#39;t work on play, but locally it works very quickly.</p></pre>dgryski: <pre><p>But blows in with the race detector. Use atomic loads instead. </p></pre>nevyn: <pre><p>Yeh, I wanted it as close as possible to the question.</p></pre>mcandre: <pre><p>Curious: Does Go allow overriding of equality according to some interface, as in C++, Java, Ruby, Haskell?</p> <p>If so, implementing an EqualTo() or such could be specially implemented to return true for inputs 1, 2, 3, ...</p></pre>PaleocorticalWise: <pre><p>Go does not allow this.</p></pre>alasijia: <pre><p>it is possible for a not-well-written concurrent program, though the possibility is very low.</p></pre>faiface: <pre><p>Hey, I know everyone&#39;s gonna hate me now, but nonsense like this can never happen in functional programming!</p></pre>TheMerovius: <pre><p>My Haskell isn&#39;t amazing, but</p> <pre><code>data Fnord = Fnord Integer instance Num Fnord where (Fnord a) + (Fnord b) = Fnord (a + b) (Fnord a) * (Fnord b) = Fnord (a * b) negate (Fnord a) = Fnord (negate a) abs (Fnord a) = Fnord (abs a) signum (Fnord a) = Fnord (signum a) fromInteger = Fnord instance Eq Fnord where a == b = True main = do a &lt;- return (Fnord 42) print (a == 1 &amp;&amp; a == 2 &amp;&amp; a == 3) return () </code></pre></pre>flogic: <pre><p>I would be careful with that. I don&#39;t think can exactly do this in Haskell, but you can play with the Eq type class for a custom type.</p></pre>faiface: <pre><p>Alright, you can possibly do that one, but you can&#39;t get this to work in Haskell:</p> <pre><code>a == 1 &amp;&amp; !(a == 1) </code></pre> <p>Which you can get in Go, JavaScript, etc.</p></pre>nhooyr: <pre><p>how can you get that in Go?</p></pre>faiface: <pre><p>Exactly as another comment described, by concurrency with shared variables and race conditions.</p></pre>nhooyr: <pre><p>That comment was deleted but I understand now, thanks.</p></pre>Koh_Phi_Phi: <pre><p>This particular problem has more to do with type safety than functional programming. For instance if you have the ability to define == as a function to always return true then functional programming doesn’t help you.</p></pre>justinisrael: <pre><p>Posted in response to this? <a href="https://codeburst.io/javascript-can-a-1-a-2-a-3-ever-evaluate-to-true-aca13ff4462d" rel="nofollow">https://codeburst.io/javascript-can-a-1-a-2-a-3-ever-evaluate-to-true-aca13ff4462d</a></p> <p>But Go doesn&#39;t allow operator overloading. </p></pre>

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

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