Is it possible define an interface type on the fly that accepts only values with types that comply to multiple given interfaces at the same time?

blov · · 385 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Let say I have:</p> <pre><code>type A interface { doAishAction() } type B interface { doBishAction() } func doABStuff(thing ???) { thing.doAishAction() thing.doBishAction() } </code></pre> <p>How should the type of <strong>thing</strong> be defined? I just want it to be something that complies <em>both</em> to <strong>A</strong> and <strong>B</strong> interfaces.</p> <hr/>**评论:**<br/><br/>usernameliteral: <pre><p>Define a third interface that implements A and B.</p> <pre><code>type C interface { A B } </code></pre></pre>svetlyo: <pre><p>Thanks!</p> <p>I wanted to define the interface type on the fly, anonymously and this works:</p> <pre><code>func doABStuff(thing interface{A; B}) { thing.doAishAction() thing.doBishAction() } </code></pre></pre>izuriel: <pre><p>You keep saying &#34;on the fly&#34; and I don&#39;t think it means what you think it means. Doing what your doing anonymously makes it harder to modify in the future if you do this in multiple places. If you &#34;predefine&#34; or name the interface in a single location it would be very easy to make modifications later on. </p> <p>Neither approach is &#34;dynamic&#34; or &#34;on the fly&#34; as they are compiled during the build phase. Unlike an interpreted language where this may actually be useful and may be dynamic or &#34;on the fly&#34; in Go I&#39;d say it would be a lot easier for long term maintenance to just define the joint interface with a name in one place. </p></pre>svetlyo: <pre><p>Named interface types represent a reusable abstraction - we want to express some well defined notion by giving a name to a set of logically related methods. But you don&#39;t have to name everything, every possible combination of your named interfaces just because at some places you need that particular combination - otherwise you&#39;ll end up with an exponential explosion of names that you have to remember (and come up with in the first place) whereas it is pretty clear what &#39;interface {Stack; Serializable}&#39; is for example (if you already know what Stack and Serializable are)</p></pre>izuriel: <pre><p>I specifically advocated for reusability over embedding anonymous types everywhere. Names are easy to remember, run &#39;go doc&#39; from your terminal on a package and get an incredibly easy and accessible method to see names. </p> <p>It seems pointless to sacrifice clarity because &#34;naming&#34; is perceived as a potentially difficult task. Having the need to constantly switch between anonymous types to avoid memorizing names smells of a potentially larger problem. At what point would a method need to know its serializing a stack? The point of a Serializer interface should be sufficient. Your methods will ultimately become very specific and less reusable as this pattern grows.</p></pre>djherbis: <pre><p><a href="https://www.youtube.com/watch?v=G2y8Sx4B2Sk" rel="nofollow">https://www.youtube.com/watch?v=G2y8Sx4B2Sk</a> :)</p></pre>613style: <pre><p>Just think of this as Reader, Writer, and ReadWriter:</p> <pre><code>type Reader interface { Read(p []byte) (n int, err error) } type Writer interface { Write(p []byte) (n int, err error) } type ReadWriter interface { Reader Writer } </code></pre></pre>

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

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