Transform Types

polaris · · 598 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Something that I&#39;ve thought about in go is the ability for a type that implements an interface to contain an instance of said interface. I&#39;ve attempted doing this with images and matrices where when doing a transformation or operation on an interface type, instead of copying to a new instance or modifying the original, you create a new implementation of the interface whose methods call those of the instance it&#39;s transforming. It seems this is a go-y way to implement lazy execution, though I haven&#39;t seen anyone talking about it.</p> <p>A simplistic example would be:</p> <pre><code>type Int interface{ Get()int } type SimpleInt int func (i SimpleInt)Get()int{ return int(i) } type PlusOne struct{ i Int } func (p PlusOne)Get()int{ return p.i.Get() +1 } </code></pre> <p>In practice you would have more complex transformations than adding one, but you get the idea.</p> <hr/>**评论:**<br/><br/>comrade_donkey: <pre><p>This is a rough homomophism to <a href="https://en.wikipedia.org/wiki/Church_encoding" rel="nofollow">Church numerals</a>, try doing it with functions. It&#39;s an interesting subject in the branch of lambda calculus and hence related to <a href="http://iml.univ-mrs.fr/%7Elafont/HETT/coquand1.pdf" rel="nofollow">type theory</a>. That said, it&#39;s a quite old research topic. It&#39;s very interesting but not very useful in and of itself (because &#34;useful&#34; deductions have already been made and part of them are already in Go&#39;s type system). </p></pre>jerf: <pre><p>I believe you&#39;re referring in general to the <a href="https://en.wikipedia.org/wiki/Decorator_pattern" rel="nofollow">decorator pattern</a>. Yes, I consider it a pretty fundamental tool. Yes, it could be used for some limited &#34;lazy execution&#34;, though I think Go programmers would not normally think of it that way. There&#39;s quite a few examples of such a thing in the core library, such as <a href="https://golang.org/pkg/io/#LimitedReader" rel="nofollow">LimitedReader</a> or <a href="https://golang.org/pkg/io/ioutil/#NopCloser" rel="nofollow">NopCloser</a>.</p></pre>

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

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