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

FR: Adding methods to classes without subclassing #34778

Open
willlarche opened this issue Oct 12, 2018 · 3 comments
Open

FR: Adding methods to classes without subclassing #34778

willlarche opened this issue Oct 12, 2018 · 3 comments
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). core-m type-enhancement A request for a change that isn't a bug

Comments

@willlarche
Copy link

Idea comes from Objective-C's "Categories".

Would be great to be able to add methods, both instance and static, to a class without having to create a new type. Here's how it works in Objective-C (I bet you could make it less verbose!) The - method is an instance method and + is the static one:

Source File: UILabel+MyNewInits.h

@interface UILabel (MyNewInits)

- (instancetype)initThatSpecialWay;
+ (instancetype)specialLabel;

@end

@implementation UILabel (MyNewInits)

- (instancetype)initThatSpecialWay {
  self = [super init];
  if (self) {
    self.text = @"Wuthering, Wuthering Heights";
  }
  return self;
}

+ (instancetype)specialLabel {
  UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
  label.text = @"Heathcliff, it's just me. It's Cathy.";

  return label;
}

@end

Consuming File: SomeView.m

#import "UILabel+MyNewInits.h"

@interface KateBushView : UIView
@end

@implementation KateBushView

- (void)awakeFromNib {
  [super awakeFromNib];

  [self addSubview:[[UILabel alloc] initThatSpecialWay]];
  // Or, like:
  [self addSubview:[UILabel specialLabel]];
}

@end
@natebosch
Copy link
Member

See language discussion: dart-lang/language#40 which has linked proposals that I think covers at least the instance method side.

cc/ @lrhn

@lrhn
Copy link
Member

lrhn commented Oct 16, 2018

This is definitely related to dart-lang/language#41 (static extension methods), but as I read it, it is actually more powerful. These extension methods (at least the instance ones) are introduced on the class the same way as members introduced by the class itself.
In Dart, that would mean that the methods can be overridden virtually in sub-classes. It also means that they can conflict with existing members or other categories on the same class.

(Static extension methods can also be extended to add static methods to classes, if that is actually useful in Dart).

@willlarche
Copy link
Author

Yeah, it's more than just static. And collisions have been a super rare occurance. They can be caught by the compiler tho.

Overriding them is a cool idea I never thought of. Seems like a less-useful case but interesting.

@a-siva a-siva added area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). type-enhancement A request for a change that isn't a bug labels Oct 19, 2018
@lrhn lrhn added the core-m label Dec 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). core-m type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants