Re: How to satisfy this interface? (fwd)
Scott Lawrence <
bytbox@...>
2012-08-08 05:35:08 GMT
On Wed, 8 Aug 2012, Jan Mercl wrote:
> On Wed, Aug 8, 2012 at 7:10 AM, Scott Lawrence <bytbox@...> wrote:
>> I think the question can be better rephrased as follows. If I'm using a
>> package with
>>
>> type ListNode struct{}
>> func (l ListNode) Next() ListNode
>>
>> and I want to create an interface `Node`, which ListNode implements, how can
>> I do so?
>
> So you are saying that having an interface type I, with method M,
> returning type R and having a non-interface type [*]T, having method
> M, returning type [*]T the question is how to attach to T another
> equally named method M returning R (i.e. [*]T then satisfies I)?
No.
An interface type I with method M returning type I. A concrete type T, method
'M', returning type T.
More generally: http://play.golang.org/p/_Wxkur9SAq. The assignability rules
require function arguments and return values to be identical, rather than the
usual (arguments must be supertypes, return values subtypes).
IIRC, this is because the go compiler has to know at compile time how large
arguments and return values passed around are going to be. Is there any reason
the rules can't be relaxed for pointer types?
--
--
Scott Lawrence
go version go1.0.2
Linux jagadai 3.4.7-1-ARCH #1 SMP PREEMPT Sun Jul 29 22:02:56 CEST 2012 x86_64 GNU/Linux