<p>Something that I've thought about in go is the ability for a type that implements an interface to contain an instance of said interface. I'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's transforming. It seems this is a go-y way to implement lazy execution, though I haven'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'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's a quite old research topic. It's very interesting but not very useful in and of itself (because "useful" deductions have already been made and part of them are already in Go's type system). </p></pre>jerf: <pre><p>I believe you'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 "lazy execution", though I think Go programmers would not normally think of it that way. There'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>