-
Notifications
You must be signed in to change notification settings - Fork 56
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 |
Working on this now. The design is a bit tricky though. Requirements:
Dart doesn't support variadic generics, so it's not obvious how to implement this. In C++ I could do something like this, and it would capture all the semantics: template <typename... Protocols> class ObjCObject : public Protocols... {}; The brute force solution is to codegen a separate class ObjCObject_A_B_C implements A implements B implements C {}
class ObjCObject_A_B implements A implements B {}
class ObjCObject_A implements A {}
class ObjCObject_B_C implements B implements C {}
class Foo implements ObjCObject_A_B {} The problem with codegenning them like this, aside from being verbose and ugly, is that one package's Another option, to get around the lack of variadic generics, would be to have a bunch of objects with specific numbers of protocols. We could add these to package:objective_c, or code gen them as needed. The trade-off here is that if we put them in package:objective_c we have to generate them up to some fixed number of protocols (eg up to 8), but if we codegen them on demand we have the same compatibility issues as above. class ObjCObject {}
class ObjCObject1<T1 extends ObjCProtocol> implements T1 {}
class ObjCObject2<T1 extends ObjCProtocol, T2 extends ObjCProtocol> implements T1 implements T2 {} As written this doesn't quite work, because we can't directly use class ConformsTo<T extends ObjCProtocol> {}
class ObjCObject {}
class ObjCObject1<T1 extends ObjCProtocol> implements ConformsTo<T1> {}
class ObjCObject2<T1 extends ObjCProtocol, T2 extends ObjCProtocol> implements ConformsTo<T1>, ConformsTo<T2> {}
class Foo implements ConformsTo<A>, ConformsTo<B> { ...
EDIT: Actually this doesn't quite capture the semantics. If |
After some investigation I don't think it's possible to capture all the semantics of protocol conformance. The best we can do is have interfaces implement every protocol they conform to, and then write methods etc that require at most one of the protocols to be implements. In other words, if we see an ObjC method that accepts // Codegen for protocols will change from abstract final to abstract interface, and extend ObjCProtocolBase.
abstract interface class ProtocolA extends objc.ObjCProtocolBase {
// Contents of protocol remains the same: only static methods/fields.
// Implementers won't actually inherit anything.
...
}
abstract interface class ProtocolB extends objc.ObjCProtocolBase { ... }
// Interfaces now directly implement all their protocols. Don't need ConformsTo under this approach.
class SomeInterface extends objc.NSObject implements ProtocolA, ProtocolB {
// Methods accepting id still take ObjCObjectBase.
void idMethod(objc.ObjCObjectBase x);
// Methods accepting id<ProtocolA> take ProtocolA directly.
void protocolAMethod(ProtocolA x);
// Methods accepting id<ProtocolA, ProtocolB> take ProtocolA. The ProtocolB is lost.
void protocolABMethod(ProtocolA x);
} |
@stuartmorgan Will a static typing solution like ^this^ work? Or are there a lot of cases where an interface implements a protocol without declaring it statically? If I have an interface |
What would be great is if Dart supported Swift's solution for this use case, which is to allow adding protocol conformance via class extensions, so you can make, in essence, strongly typed ducks2. Obviously that would be a language level feature though, not a bindings generator feature. Footnotes
|
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: