4 ms·
Yea, as mentioned by vectorjohn, you actually just create an interface for whatever you need. So in your example, you might have an Adder interface that simply
by fizzbatter 11y ago
Yea, as mentioned by vectorjohn, you actually just create an interface for whatever you need.
So in your example, you might have an Adder interface that simply checks for the Add() method, but if you know you're going to need VectorAddition, Addition, and Appendable, make a new interface that implements all three, then use that.
You can do this in one large interface, or 3 small interfaces that you use to compose a 4th, larger interface (usually the preferred way). Example:
type Adder interface {
Add()
}
type VectorAdder interface {
VectorAdd()
}
type Appender interface {
Append()
}
type VectAddAppendable interface {
Adder
VectorAdder
Appender
}
Make sense? Forgive any lapses in syntax, it's been a long day. But i think this is the solution you were referring to correct? By saying that the object coming in must implement the VectAddAppendable interface, you can be positive that it implements the methods you care about.
A good example of this is in Golang's stdlib IO package. There's Reader, Writer, and ReadWriter. ReadWriter simply requires both the Reader and Writer interfaces. It's a very common paradigm.
Does that solve your issue?