How do you deal with "No Generics"?

polaris · · 803 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Do you miss them or what tools or patterns you use to counter the lacking?</p> <hr/>**评论:**<br/><br/>rogpeppe: <pre><p>Go may not have generics, but it&#39;s perfectly possible (and natural) to write generic code with it. For example, any function that takes an interface type is generic. There are many examples of this in the Go standard library.</p></pre>afrobee: <pre><p>Yes, but that is not type safe as I may hear so many times.</p></pre>rogpeppe: <pre><p>The non-type-safety comes when you have some kind of a container and you need to assert that something coming out of the container is the same type of thing that you put into it. There is lots of generic code (arguably most) that doesn&#39;t need to do this.</p> <p>Consider, for example, the function ioutil.ReadAll:</p> <pre><code>func ReadAll(r io.Reader) ([]byte, error) </code></pre> <p>this is perfectly generic (you can pass it anything that implements the io.Reader interface) and also type safe.</p></pre>afrobee: <pre><p>Oh great, I was just thinking about the empty interface, for some reason.</p></pre>mwholt: <pre><p>Interfaces are completely type-safe. They&#39;re checked at compile-time.</p></pre>egonelbre: <pre><p>I miss generics sometimes, but then write the non-generic version in lesser time and get on with my programming. :)</p> <p>Also <a href="https://docs.google.com/document/d/1vrAy9gMpMoS3uaVphB32uVXX4pi-HnNjkMEgyAHX4N4" rel="nofollow">https://docs.google.com/document/d/1vrAy9gMpMoS3uaVphB32uVXX4pi-HnNjkMEgyAHX4N4</a></p></pre>avrtno: <pre><p>The <a href="http://godoc.org/sort" rel="nofollow">sort package</a> is an excellent example of how to write type safe code without generics.</p></pre>garfj: <pre><p>Largely I&#39;ve found that it&#39;s not too difficult to get by without them with a bit of thought towards designing the right <code>interface</code>. </p> <p>That being said, there are still patterns that I like to encapsulate in &#34;more generic&#34; terms, and as such I&#39;ve been working on code-generation based meta-programming tool (<a href="https://github.com/go-goast/goast" rel="nofollow">goast</a>) that supports generics-like patterns.</p> <p>I&#39;ve found the first thing people want when they reach for generics is abstracted array traversal, which you can find in the <a href="https://github.com/go-goast/iter" rel="nofollow">iter</a> package.</p> <p>That being said I find the utility of abstracting something like a concurrent process pipeline (Fan Out/Fan In) to be much more interesting and useful. You can find that in the <a href="https://github.com/go-goast/pipeline" rel="nofollow">pipeline</a> package.</p></pre>kpmy: <pre><p>Generics present a tool, that humanity invented, but did not undestood. Generics, in general, perform uncontrollable changes of your code. If something changes your code, it&#39;s not your anymore. These changes differ from compiler optimisations, that can be disabled without effect. </p></pre>Raiyni: <pre><p>You either make a function for each type that you want to support or implement it using interfaces and use reflection. </p></pre>: <pre><p>[deleted]</p></pre>SingularityNow: <pre><p>Help a brother out so that he&#39;s less of an idiot in the future. </p> <p>I&#39;ve been riding along with the impression that generics were all about reducing manual code duplication by capturing higher level patterns in a single location so they can be modified and maintained easily.</p> <p>My understanding was that various languages handle this in different ways. If look at how C++ handles it (compile-time code generation via templates), and how C# handles it (run-time code generation [<a href="https://msdn.microsoft.com/en-us/library/f4a6ta2h.aspx" rel="nofollow">source</a>]) , it seems the generation of code at least comes into play sometimes.</p> <p>Furthermore it also <em>seems</em> that the Go Authors are saying that the generation of type-specialized code from a generic package is a reasonable usage of the <code>go generate</code> command (<a href="https://docs.google.com/document/d/1V03LUfjSADDooDMhe-_K59EgpTEm3V8uvQRuNMAEnjg/edit" rel="nofollow">go generate proposal</a>, first page <em>&#34;macros: generating customized implementations given generalized packages, such as sort.Ints from ints&#34;</em>).</p> <p>So, like I said, I would actually really like to gain a deeper understanding of your argument, but your sole supporting &#34;fact&#34; of &#34;they&#39;re idiots&#34;, isn&#39;t really doing me any favors.</p></pre>

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

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