anonymous function syntax could be improved

agolangf · · 510 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>The current syntax is clumsy, I would love to see a reduced, better readable way to write anonymous functions.</p> <p>Current:</p> <pre><code>type someFn func(int, int) int func foo(n int) someFn { return func(a, b int) int { return a + b + n } } </code></pre> <p>Using the &#34;type alias&#34; could be an improvment.</p> <pre><code>func foo(n int) someFn { return someFn{ return a + b + n } } </code></pre> <p>Keeping somewhat the function signature would be not bad. Therefore lets try to keep it, but still remove some clutter. Lets start with not having to type <code>func()</code> at the beginning of every anonymous function.</p> <pre><code>func foo(n int) someFn { return (a, b int) int { return a + b + n } } </code></pre> <p>Since the return type of the function is known, I would love to not have to write <code>return ...</code> inside the anonymous function. If the function returns a value, just return the last computed value (for early-return, use the return keyword).</p> <pre><code>func foo(n int) someFn { return (a, b int) int { a + b + n } } </code></pre> <p>The function signature is know in the case of someFn, therefore an <strong>alternative</strong> anonymous function syntax would be nice, replacing <code>(a, b int) int {...</code> with <code>{a, b int -&gt;...</code>.</p> <pre><code>func foo(n int) someFn { return {a, b int -&gt; a + b + n } } </code></pre> <p>It would be even possible to remove the <code>int</code> part of the signature.</p> <pre><code>func foo(n int) someFn { return {a, b -&gt; a + b + n } } </code></pre> <p>Some languages – like Kotlin – have an optional dedicated variable name for anonymous function with only one parameter called <code>it</code> (it stands for item).</p> <pre><code>func addOneFn() func(int) int { return { it + 1 } // instead of: { x -&gt; x +1 } } </code></pre> <p>What do you think, does go need a better syntax and how should it look?</p> <hr/>**评论:**<br/><br/>TheMerovius: <pre><blockquote> <p>What do you think, does go need a better syntax</p> </blockquote> <p>No. This is, if anything, a very minor issue (I don&#39;t consider the current syntax &#34;clumsy&#34;, at all) and not worth complicating the language. Pretty much everything you suggest would significantly complicate the parser and spec. </p></pre>Paradiesstaub: <pre><blockquote> <p>Pretty much everything you suggest would significantly complicate the parser and spec.</p> </blockquote> <p>I see your point of being okay with the current syntax. But the &#34;would complicate the parser&#34; part is kind of strange to me, because that&#39;s an argument against every change.</p></pre>TheMerovius: <pre><blockquote> <p>But the &#34;would complicate the parser&#34; part is kind of strange to me, because that&#39;s an argument against every change.</p> </blockquote> <p>a) That&#39;s not true, there is the possibility to simplify the parser with a change.</p> <p>b) Even if it where true, not every change complicates it <em>significantly</em>. You are suggesting removing a bunch of keywords from certain parts of the language, which means the parser now needs to infer from the context what was meant, by looking at more tokens and add a bunch of extra logic. Keywords serve an important purpose in the language, as they can remove context-sensitivity from the language.</p> <p>c) And lastly, yes, it is an argument against most changes. Meaning, every change needs to have benefits that outweigh this argument against them.</p></pre>callcifer: <pre><blockquote> <p>that&#39;s an argument against every change.</p> </blockquote> <p>That&#39;s exactly why a lot of &#34;improvements&#34; like these get rejected all the time. Anything that complicates the parser without a <em>significant</em> benefit to the language is automatically rejected.</p> <p>What you are proposing is an alternative syntax for doing the same thing, which Go avoids almost everywhere (e.g, <code>{</code> on the same line as <code>if</code>, only <code>for</code> for loops - no <code>do</code> or <code>while</code>, no ternary operators). </p></pre>LimEJET: <pre><p>The thing with Go is that there are no syntax alternatives. That&#39;s what makes the spec so small and the language so simple to pick up.</p> <p>A function looks like a function, no matter where it appears.</p></pre>andradei: <pre><p>&gt; The thing with Go is that there are no syntax alternatives</p> <p>Except for variable definition. Which Rob Pike said should go away, but it&#39;s too late to do so.</p> <p>var n int = 5 var n = 5 n := 5</p></pre>LimEJET: <pre><p>Indeed. The shorthand is nice though, and you end up using each type in specific situations:</p> <ul> <li>Globals: <code>var thing = 1</code></li> <li>Locals without assignment: <code>var thing int</code></li> <li>Locals with assignment: <code>thing := 1</code></li> </ul> <p>And the last one (<code>var thing int = 1</code>) only gets used when type inference would fail.</p></pre>peterbourgon: <pre><p>These all seem like much worse options.</p></pre>PsyWolf: <pre><p>I strongly recommend watching Rob Pike&#39;s talk <a href="https://www.youtube.com/watch?v=rFejpH_tAHM" rel="nofollow">Simplicity is Complicated</a>. The whole thing is worth watching but <a href="https://youtu.be/rFejpH_tAHM?t=311" rel="nofollow">this part</a> in particular is the main reason go doesn&#39;t have 2 different ways to declare functions (or do much else for that matter). </p> <p>Anonymous function declarations look almost exactly like normal function declarations. No special &#34;-&gt;&#34; or &#34;/&#34; lambda syntax. No implicit dedicated variables like &#34;it&#34;. It&#39;s just the same boring syntax you use everywhere else but without the function name. Not having 2 different ways to do everything is why we love this language. We wont change that to save a few keystrokes.</p></pre>Paradiesstaub: <pre><p>The talk about simplicity was very interesting, thanks for posting the link!</p></pre>flat20: <pre><p>I think these examples sound like something your favourite editor could expand into go code.</p></pre>justinisrael: <pre><p>I really appreciate the clarity of the way it is now. To me, having alternate context based syntax is more cognitive load. I prefer the smaller language and the consistency. A function always reads as a function. </p></pre>dlsniper: <pre><p>I can currently extract every anonymous function into a function or method pretty much with a few small changes to the signature only. What you propose is just more work down the road for a few saved keystrokes. Imho you should learn more about the editor you are using and find out how you can do that with it.</p></pre>trisjpheck: <pre><p>Bikeshedding in action.</p></pre>ultra_brite: <pre><p>why do you bother with all this? the syntax isn&#39;t going to change whether you want it or not. The mailing list is full of feature discussions that went nowhere because Go maintainers refuse to change the syntax of the language.</p></pre>Paradiesstaub: <pre><p>Why, because I use closures and think that I type too much to get one.</p></pre>mistretzu: <pre><p><a href="http://www.keybr.com" rel="nofollow">This approach</a> might be more pragmatic than trying to change the go syntax :)</p></pre>ultra_brite: <pre><p>But Go maintainers don&#39;t care, they won&#39;t change the language so you can save key strokes. There is a ton of more useful proposals out there and they are all rejected by the Go team, because they don&#39;t wont to change the language that&#39;s all there is to it. Don&#39;t waste your time and accept that Go won&#39;t change ... or use something else.</p></pre>

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

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