dart-lang / dart-lang/native

Does anyone need to implement protocol class methods?

Open
#1,149 1 comment 0 reactions 0 assignees View on GitHub
lang-objective_c package:objective_c type-enhancement
Dominant language
Dart
Stars
275
Forks
144
Avg merge
2d 10h
Merged PRs (30d)
47

Description

My initial design for [implementing ObjC protocols from Dart](https://github.com/dart-lang/native/issues/1040) 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:

```objective-c
@protocol MyProtocol
+ (double)classMethod;
@end

@interface ObjCProtocolImpl : NSObject
@end

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

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

@implementation ProtocolConsumer : NSObject
- (double)callClassMethod:(id)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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.