Skip to content
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

Does anyone need to implement protocol class methods? #1149

Open
liamappelbe opened this issue May 16, 2024 · 1 comment
Open

Does anyone need to implement protocol class methods? #1149

liamappelbe opened this issue May 16, 2024 · 1 comment
Labels
lang-objective_c Related to Objective C support package:objective_c type-enhancement A request for a change that isn't a bug

Comments

@liamappelbe
Copy link
Contributor

My initial design for implementing ObjC protocols from Dart will only support instance methods. This is the use case most Dart programmers will be familiar with, analogous to implementing the methods of an interface.

But in ObjC, protocols can also contain class methods (analogous to static methods in Dart). It's possible to implement them, and for a consumer of the protocol to get the instance's class and then call the implementation of that class method:

@protocol MyProtocol<NSObject>
+ (double)classMethod;
@end


@interface ObjCProtocolImpl : NSObject<MyProtocol>
@end

@implementation ObjCProtocolImpl
+ (double) classMethod {
  return 1.23;
}
@end


@interface ProtocolConsumer : NSObject
- (double)callClassMethod:(id<MyProtocol>)proto;
@end

@implementation ProtocolConsumer : NSObject
- (double)callClassMethod:(id<MyProtocol>)proto {
  Class cls = [proto class];
  return [cls classMethod];  // Returns 1.23
}
@end

Supporting this would be tricky, and I think that protocols with class methods are rare. So for now I'm not going to support it. Comment on this issue if you have a use case.

@liamappelbe liamappelbe added type-enhancement A request for a change that isn't a bug lang-objective_c Related to Objective C support package:objective_c labels May 16, 2024
@liamappelbe liamappelbe moved this to Backlog in ObjC/Swift interop May 16, 2024
@dcharkes
Copy link
Collaborator

Off-topic: I've wanted to have virtual statics and virtual constructors in Dart for a long time (dart-lang/language#356)! Cool to see that Objective-C has them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lang-objective_c Related to Objective C support package:objective_c type-enhancement A request for a change that isn't a bug
Projects
Status: Backlog
Development

No branches or pull requests

2 participants