-
Notifications
You must be signed in to change notification settings - Fork 246
Description
Consider the following WIT file:
package test:component;
interface i {
resource r;
enum kind { a, b, c }
f: func(k: kind) -> r;
}
// wrapped-i is exactly the same as i
interface wrapped-i {
resource r;
enum kind { a, b, c }
f: func(k: kind) -> r;
}
world root {
import i;
import wrapped-i;
export i;
export wrapped-i;
}
In wit-bindgen, kind will be defined four times: test::component::i::Kind, test::component::wrapped_i::Kind, exports::test::component::i::Kind, and exports::test::component::wrapped_i::Kind. It would be good to have an option to say that these four kind are the same, and we only define it once, the remaining module will use type alias instead. This avoids value cloning when we want to pass kind from one interface to another. This only works for types which contain no resources. With the lazy value proposal, this may change though.
I can see three options:
-
add a
interface_groupflag, so that we can sayimport.i,import.wrapped-i,export.i,export.wrapped-iare in the same group, and the bindgen will find all non-resource types in the interface and make sure they are defined only once. -
We could also fix this by extending the WIT syntax, something like
world root {
import i;
import i as wrapped-i;
export i;
export i as wrapped-i
}
Then wit-bindgen will know that the non-resource types in these four interfaces are the same just by looking at the WIT file.
- add a
type_eqflag, and sayimport.i.kind,import.wrapped-i.kind,export.i.kindandexport.wrapped-i.kindare equal. This allows more fine grained control, and can handle cases where the two interfaces are not exactly the same, but we still want to share some data types.
It feels like that the interface level equality is better fixed from the WIT syntax, but the type level equality is a suitable extension in wit-bindgen alone and can also cover the interface equality case without waiting for a syntax change.