英文:
struct initialize to satisfy an interface without explicit method definition
问题
给定伪代码如下:
type(
MyStruct struct {
AFunction func(string) ([]byte, error)
}
MyInterface interface {
AFunction(string) ([]byte, error)
}
)
func NeedThis(s string) ([]byte, error){
//相关函数内容
}
m := &MyStruct{AFunction: NeedThis}
问题出在m不满足MyInterface接口;我可以理解为什么会这样。有没有一种方法可以以这样的方式将函数附加到结构体上,以便构造的结构体满足接口,而无需在结构体上定义方法?我对此有一些模糊/错误的推理,也许可以帮助我澄清这一点,或者展示一种更好的推理这种情况的方法。
英文:
Given pseudo go code below:
type(
MyStruct struct {
AFunction func(string) ([]byte, error)
}
MyInterface interface {
AFunction(string) ([]byte, error)
}
)
func NeedThis(s string) ([]byte, error){
//relevant function stuff
}
m := &MyStruct{AFunction: NeedThis}
The problem arises that m does not satisfy the MyInterface interface; and I can somewhat see why this would be so. Is there a way to attach functions to a struct in such a way that the constructed struct satisfies an interface without actually building out defining methods on the struct? I'm having some shadowy/faulty reasoning around this, maybe help to clarify this for me or show a better way to reason through this situation.
答案1
得分: 0
你能不能在MyStruct
上定义一个名为AFunction
的方法,然后在该方法中调用存储的AFunction
函数指针?如果你有很多这样的情况,这并不是理想的解决方案,但我认为它可以完成工作。
例如:
func (s MyStruct) AFunction(str string) ([]byte, error) {
return s.AFunction(str)
}
编辑:上述代码可能会导致编译器错误,因为s.AFunction
是模棱两可的,所以你可能需要给它们(方法和函数指针)分配不同的名称,但这应该能给出正确的思路。
英文:
Could you not just define a method AFunction
on MyStruct
that dispatches to the stored AFunction
function pointer? It's not ideal if you have a lot of these, but I think it does the job?
i.e. something like
func (s MyStruct) AFunction(str string) ([]byte, error) {
return s.AFunction(str)
}
edit: the above may cause the compiler to error because s.AFunction
is ambiguous, so you might have to give them (the method and the function pointer) different names, but it should give the right idea.
答案2
得分: 0
你可以将其包装在另一个实现接口的结构体中:
package main
import "fmt"
type (
MyStruct struct {
AFunction func(string) ([]byte, error)
}
MyInterface interface {
AFunction(string) ([]byte, error)
}
)
func NeedThis(s string) ([]byte, error) {
//相关函数的内容
return nil, nil
}
type Proxy struct {
*MyStruct
}
func (x *Proxy) AFunction(s string) ([]byte, error) {
return x.MyStruct.AFunction(s)
}
func main() {
m := &MyStruct{AFunction: NeedThis}
p := &Proxy{m}
_, ok := MyInterface(p).(MyInterface)
fmt.Println(ok)
}
请注意,这是一个Go语言的代码示例。
英文:
You can wrap it in another struct that implements the interface:
http://play.golang.org/p/AgnYAWBdUp
package main
import "fmt"
type (
MyStruct struct {
AFunction func(string) ([]byte, error)
}
MyInterface interface {
AFunction(string) ([]byte, error)
}
)
func NeedThis(s string) ([]byte, error) {
//relevant function stuff
return nil, nil
}
type Proxy struct {
*MyStruct
}
func (x *Proxy) AFunction(s string) ([]byte, error) {
return x.MyStruct.AFunction(s)
}
func main() {
m := &MyStruct{AFunction: NeedThis}
p := &Proxy{m}
_, ok := MyInterface(p).(MyInterface)
fmt.Println(ok)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论