impl icon indicating copy to clipboard operation
impl copied to clipboard

Add missing functions when running impl twice

Open marcosnils opened this issue 7 years ago • 3 comments

It'd be awesome if impl could add the missing methods of an interface to a struct when executed twice. What it currently does is just duplicating all the methods.

If this makes sense I can try to craft a PR.

marcosnils avatar Jun 22 '17 13:06 marcosnils

This seems like a reasonable request. (It is a trimmed back version of #2, but trimmed back in all the right ways.)

Suggested approach:

  • Add a -incremental flag, use it to enable this new behavior. Alternatively, add a -all flag to disable the behavior and have incremental on by default.
  • If you can't find an existing type that matches the provided type, pretend it exists and has no methods. In particular, don't emit an error in this case.

There is some subtlety around pointer vs non-pointer receivers. Suppose you call impl with an interface containing methods A() and B(), and you provide T as the type, and *T already has a method B(). Should impl generate func (t T) B() { panic(...) }? If yes, then you'll get a method redeclared error: https://play.golang.org/p/fNIjtt-ISj. If not, T won't satisfy the interface, despite your explicit request that it do so: https://play.golang.org/p/8sp05N9zg-. Or should it return an error, which I try to avoid at all costs?

I'd lead towards the method redeclared approach, on the grounds that it is the most likely to make it obvious to the user what has gone wrong. Thoughts welcomed, though.

josharian avatar Jun 23 '17 02:06 josharian

This is exactly what i need, i suppose it has not been implemented or anything? @marcosnils did you do some work on it, or no?

DenLilleMand avatar Jan 03 '19 10:01 DenLilleMand

implemented in PR#36 but I didn't add -incremental parameter to save compatibility with current IDE logic.

timsolov avatar May 21 '20 14:05 timsolov