newc icon indicating copy to clipboard operation
newc copied to clipboard

let return value's type to be an interface

Open lostpg opened this issue 2 years ago • 2 comments

Like the struct myDoer below:

type Doer interface {
    Do() (int, error)
}

type myDoer struct {
    v int
}

func (r *myDoer) Do() (int, error) {
    // ...
    return 0, nil
}

And myReader's constructor sig shall be like:

func NewMyDoer(v int) Doer

instead of

func NewMyDoer(v int) *myDoer

It can be an option, and user can choose its constructor to return an pointer or an designated interface.

lostpg avatar Nov 16 '23 08:11 lostpg

An interesting idea. But I'm not sure if it's feasible, because I think it would be difficult to identify at the syntax tree level which structs implement which interfaces. Does anyone else have any thoughts on implementation?

Bin-Huang avatar Mar 15 '24 08:03 Bin-Huang

An interesting idea. But I'm not sure if it's feasible, because I think it would be difficult to identify at the syntax tree level which structs implement which interfaces. Does anyone else have any thoughts on implementation?

"I also really need this feature because I want to use it with Wire. I think it would be great to add a parameter to specify the interface to bind, just like Wire does, for example: newc -i=Doer."

springeye avatar Aug 09 '24 09:08 springeye