Hello!
To explain what I meant in the title, I have an Observable
class,
@Observable
final class ModelProvider { ... }
Now I would like to make an EnvironmentValues
for this class, something like this:
extension EnvironmentValues {
var modelProvider: ModelProvider {
get { self [ModelProviderKey.self] }
set { self [ModelProviderKey.self] = newValue }
}
private struct ModelProviderKey: EnvironmentKey {
static var defaultValue: ModelProvider = .instance
}
}
I wrote my extension macro, (in fact, the above code was generated by the macro), but the compiler complains Extending a protocol composition is not supported; extending 'ModelProvider' instead
.
However, in the screenshot,
there aren't any nested extensions, hence I support there must have been a compiler bug, which is understandable, extension macros are designed for extending the type it is attached to, aka,
ModelProvider
.
Then how can I define an extension on EnvironmentValues
using macros? I saw someone on the forum suggesting something like this:
@Add(ModelProvider.self)
extension EnvironmentValues {}
But I wonder if there is a way to do this using just one macro?
Any advice would be greatly appreciated! Thank you in advance.