Repository navigation
Description
(Just realized I never made a public github issue for this, creating now for some preliminary discussion as I've started working on the implementation)
As part of dagger/dagger#6102 (comment) we realized that support for interfaces will enable some important use cases that would otherwise require opening up the gates to dependency version hell (see the discussion on the linked PR for more details on the dependency hell problem). Desire for interface support has also come up quite a bit previously in other contexts.
Examples of usefulness:
- Modules can accept types from other modules without needing an explicit dependency on them (or even awareness of them at all)
- The use case described here is a good example
- Similar ones to the above have also come up from other users, not just a one-off
- We have quite a few repetitive APIs in core such as
exportandpublishthat are all defined on different types; giving clients the ability to be agnostic of the underlying type may be beneficial to both user code and even our own internal code (such as the CLI). - Our existing requirements around what an SDK module is (a module that has
ModuleRuntimeandCodegenfunctions w/ certain signatures) could be formalized and enforced more gracefully - Interfaces may give routes to mocking out objects for the purposes of testing (though this idea in particular is still in the "vague" stage)
Here's a toy example of what should become valid code with this support in place (using Go, but in general is language agnostic):
Mallard module (no deps):
type Mallard struct{}
func (Mallard) Quack() string {
return "mallard quack"
}
Crested module (no deps):
type Crested struct{}
func (Crested) Quack() string {
return "crested quack"
}
Pond module (no deps):
type Pond struct {
Ducks []Duck
}
type Duck interface {
Quack(context.Context) (string, error)
}
func (m Pond) WithDuck(duck Duck) Pond {
m.Ducks = append(m.Ducks, duck)
return m
}
func (m *Pond) QuackAll(ctx context.Context) (string, error) {
var quacks []string
for _, duck := range m.Ducks {
quack, err := duck.Quack(ctx)
if err != nil {
return "", err
}
quacks = append(quacks, quack)
}
return strings.Join(quacks, "\n"), nil
}
Caller module (deps on Mallard, Crested and Pond):
type Caller struct{}
func (m * Caller) Test(ctx context.Context) (string, error) {
return dag.Pond().
WithDuck(dag.Mallard()).
WithDuck(dag.Crested()).
QuackAll(ctx)
}
Activity
sipsma commentedon Dec 4, 2023
One tangential issue that will likely come out of this is that we may want to (backwards-compatibly) adjust how opts are accepted in the Go SDK.
The problem is that right now non-required function args get grouped into a concrete struct
*Opts.I think we probably will want to change that from a concrete struct to an interface definition. We will still generate the concrete
*Optsstruct too but it will just implement the interface that is accepted. This should be a backwards compatible change: https://go.dev/play/p/KNquXNEuuyxIf we don't do that, it will be impossible to define an interface that accepts a object with signatures including non-required args.
This idea is still incomplete though; we need to also ensure that the interface accepted for Opts doesn't itself include the concrete struct or anything like that. One option is to just use
any, which is meh but may be okay. But it's worth more thought; I haven't invested a ton of time on this path yet, just wanted to call it out early. There's almost certainly more routes available to solving this problem too, but will cross that bridge when we get there.sipsma commentedon Dec 4, 2023
Another thing that came up in previous discussion was whether this means we're ruling out languages that don't support interfaces (or otherwise are an awkward fit).
I actually don't think that should be too hard to deal with. SDKs for a given language can also implement equivalent support for this by only using concrete types. The basic outline would be:
For SDKs that do support notions of interfaces (and equivalent duck-typing concepts), that's essentially what they'll be doing underneath the hood anyways. So the only difference is that languages w/out an interface-like concept will require their users be a bit more explicit about conversions and have to rely slightly more on runtime type checking than compile time. But that seems like a perfectly fair tradeoff.
helderco commentedon Jan 5, 2024
Sorry for unintended sync issue with Linear.