Kind-of Generics

xuanbao · · 606 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>One of the things I love about Golang is, actually, the lack of generics. Having a type for every variable and being bound to it just feels like you&#39;re writing higher quality code. There are times though where I&#39;ll be forced to do a type conversion and think what&#39;s the point, and how nice it would be to have truly mutable variables.</p> <p>It seems there&#39;s two options that are being talked about; either have strict typing, or have generics. The proposals for generics too look more in the C++ fashion, whereas I&#39;m thinking more JavaScript.</p> <p>What if there could be a middle-ground?</p> <p>I am by no means knowledgeable enough to comment on the inner working of Go or it&#39;s compiler or anything, but brainstorming, it would be pretty interesting to be able to define multiple types a variable could be. For instance, if a function needs to accept both an int and float32 value, you could define the parameter with these two possible types.</p> <p>To do this, I&#39;m not sure if anything would even need to be changed on the actual Go side of things (although I&#39;m sure a standard implementation would be much better), but I believe this could be done as a kind of transpiler - Go code with multiple types is taken and transformed into standard Go code (multiple functions, etc).</p> <p>Sometimes things are better explained with code:</p> <p>What we have now (Ex #1): <a href="http://play.golang.org/p/Ofqmu7wEhM" rel="nofollow">http://play.golang.org/p/Ofqmu7wEhM</a></p> <p>What KoG (Kind-of Generics) could look like (Ex #2): <a href="http://play.golang.org/p/30Jv8pSfL3" rel="nofollow">http://play.golang.org/p/30Jv8pSfL3</a></p> <p>How the final transpiled Go code could look for Ex #2: <a href="http://play.golang.org/p/ZwDkeNoWIT" rel="nofollow">http://play.golang.org/p/ZwDkeNoWIT</a></p> <p>Just thinking about it and I&#39;m not proposing this as a change or anything, would just like to see what other people think.</p> <hr/>**评论:**<br/><br/>shovelpost: <pre><p>If you <em>truly</em> need &#34;Kind-of Generics&#34; then use go generate. </p></pre>beeker1121: <pre><p>Haven&#39;t heard of go generate before, looks interesting - is it basically a way to include your own functions within the compiler, that you can call from your Go code? I&#39;m not sure I fully understand it.</p></pre>shovelpost: <pre><p>You can read the basics <a href="https://blog.golang.org/generate" rel="nofollow">here</a>.</p></pre>beeker1121: <pre><p>Thanks, gonna check it out.</p></pre>interactiv_: <pre><p>C has void pointers, Go has interface {} . Go stdlib is full of interface {} in method signatures. You know what it&#39;s fine to use that. Go gen is just nasty and doesn&#39;t even solve the &#34;generics&#34; problem. Generics are a type you can pass to methods (in languages that support generics) , code generation doesn&#39;t add support for generics in Go. I don&#39;t believe text templating (because that&#39;s exactly what Go generate is about) produces higher quality code, because now you have to maintain the generator, the template and the pipeline to generate the code.</p> <p>And you know what ? that&#39;s actually the job of a compiler at first place ! Now you get to write your own compilers because Go designers can&#39;t be bothered with that.</p></pre>barsonme: <pre><p>I prefer code generation, tbh.</p> <p>The whole generation pipeline is fairly simple, especially for larger projects. For example, at work we will have a <code>make.go</code> file in the root directory that builds the entire app. Adding a generation phase is as easy as adding a new flag.</p> <p>Plus, there&#39;s a handful of &#34;generic&#34; libraries out there that support code generation, so you don&#39;t have to maintain that either.</p> <p>I dislike generics implemented with <code>interface{}</code> because generics you lose type safety. An <code>Add(X, y interface{}</code> function accepts strings and structs and slices as well as integer and floating point types.</p> <p>This means either the function needs to return the value <em>and</em> an error, or it needs to panic on an invalid input. The former is annoying and the latter is a terrible programming practice</p></pre>interactiv_: <pre><p>i care about polymorphism more than I care about Go rigid type system. Code generation is not polymorphism.</p></pre>beeker1121: <pre><p>Good point, and if the text templating is something like how C++ handles generics, I completely agree that it seems backwards. If anything, generics imo would be better implemented either in partial as the KoG example, or like JavaScript.</p></pre>interactiv_: <pre><p>What they should have done, and it would have been a good compromise, is to allow &#34;parametric&#34; functions :</p> <pre><code>func Foo&lt;T&gt;(arg1 T)T{ ... } Foo&lt;int&gt;(3) // returns int </code></pre> <p>This way structs and co aren&#39;t affected, this is exactly how append works by the way, Go knows the return type of append, how ? I don&#39;t know, same with make. Why users are forced to use &#34;void pointers&#34; when go functions can do that is beyond me.</p> <p>But it doesn&#39;t really matter, people have been arguing for years about the topic, nothing will be done about it, just use &#34;interface {}&#34; or &#34;[]interface {}&#34; and assert the resulting type of the value or the element of the slice. But don&#39;t use Go generate that&#39;s a mistake if your goal is clean code.</p></pre>mc_hammerd: <pre><p>agreed</p> <p>i wrote a bash script to do just this; its a little inconvenient you have to save your go files as a diff extension (or foo_template.go) and use <code>//+build ignore</code>... then generate them. i had to learn awk and sed (from the 1970s!) to write it, it took a day but it worked really well.</p> <p>i had planned to share my script but my hd exploded. <code>ginny</code> was the best go generate tool ive used; it does 100% what you need</p></pre>beeker1121: <pre><p>Pretty cool, thanks for the info.</p></pre>jasonMq: <pre><p>&#34;Having a type for every variable and being bound to it just feels like you&#39;re writing higher quality code&#34; ehh..nope..sorry I can&#39;t keep reading after that...</p></pre>beeker1121: <pre><p>Ultimately personal opinion, but I think a lot of people would agree with the statement.</p> <ol> <li>You as the programmer have to spend more time in the planning stages, so ideally more thought goes into your code.</li> <li>In a statically typed language, more checks are performed by the compiler, resulting in more bugs found before they occur at runtime - better code.</li> <li>Using static types saves on memory allocation.</li> <li>No need to check for types by the compiler (or interpreter).</li> </ol> <p>I guess you could phrase it as &#39;writing better applications&#39; if you want.</p></pre>

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

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