-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Obj-C protocol conformance is dropped from types #1462
Comments
We can experiment with this, but I held off doing it in the initial implementation for a few reasons:
|
Do you have an example of this to illustrate the kind of problem that would happen?
Can you elaborate on this one? Are you talking about
I think starting with just the type would be a big improvement. (Type and required methods would be even better, for better code completion and Dart-side IDE experience.)
I can't remember ever seeing that used in practice, so it probably wouldn't matter much what happened. |
An example for both 1 and 2: @protocol SomeProtocol<NSObject>
@required
- (int32_t)instanceMethod;
@end
@interface UndeclaredImplementation : NSObject
- (int32_t)instanceMethod;
@end
@implementation UndeclaredImplementation : NSObject
- (int32_t)instanceMethod {
return 123;
}
@end
@interface Consumer : NSObject
- (int32_t)callInstanceMethod:(id<SomeProtocol>)protocol;
@end
@implementation Consumer : NSObject
- (int32_t)callInstanceMethod:(id<SomeProtocol>)protocol {
return [protocol instanceMethod];
}
@end
int32_t foo() {
return [[Consumer new] callInstanceMethod: [UndeclaredImplementation new]];
} This compiles and runs, and All I get from clang is a warning:
Whether or not this matters depends on whether there are any APIs that do this in practice. I suppose we can just put this behind a config flag, so users can opt out if necessary. |
It's Objective-C; almost any type violation will just give you a warning. I would not expect to be able to write If "possible to write/run in Obj-C" were our criteria for mirroring, then we would need to throw away the whole type system, not just protocols. This code also builds and runs with nothing but warnings, for instance: NSArray* notAnArray = @"actually a string";
[self thisParamTypeIsString:notAnArray];
The code in your example isn't an API, it's an implementation. I don't see any problems in the APIs in that code. Unless you mean that |
I got around this issue by creating a "proxy" class which takes the protocol object in the init function and calls the methods on that object. For example, for @objc public class FlutterTextureRegistryProxy: NSObject {
public init(textures: FlutterTextureRegistry) {
self.textures = textures
}
let textures: FlutterTextureRegistry;
@objc public func registerTexture(texture: any FlutterTexture) -> Int64 {
return self.textures.register(texture);
}
@objc public func textureFrameAvailable(textureId: Int64) {
return self.textures.textureFrameAvailable(textureId);
}
@objc public func unregisterTexture(textureId: Int64) {
return self.textures.unregisterTexture(textureId);
}
} But it would be great to have the protocols be generated to avoid this hassle |
@kekland I think your work around is solving a different problem. Are you trying to make a protocol invocable? My fix probably won't help with that. The fix I'm planning will add a Dart interface for each protocol, have the classes implement those interfaces, and then pass around those interfaces instead of I guess if we want to support methods that return protocols, the protocol interface can't actually be abstract. I'll have to make it also be a concrete implementation of the protocol, so that I have something to construct and return. This will also act as an escape hatch for users to work around the type restrictions if necessary. I suppose if we code gen all the required methods, then this would make the protocol invocable. |
@liamappelbe ah, you're right, was a long day yesterday :D but yes, it would be great to have both of those things |
While migrating plugin code from native to Dart, at one intermediate stage I had this:
which was turned into:
Without any of the protocols in the types, it's incredibly easy to call incorrectly, and also much less self-documenting. Ideally Obj-C protocols would be reflected as Dart abstract interfaces that are
implements
'd.The text was updated successfully, but these errors were encountered: