native icon indicating copy to clipboard operation
native copied to clipboard

ObjC: Support a constructor for all NSObject subclasses

Open brianquinlan opened this issue 3 years ago • 1 comments

NSObject has a new class method that is equivalent to [[class alloc] init] but calling it is not intuitive because new is a keyword in Dart.

Maybe we could have a special case like this:

class MyClass extends NSObject {
  factory MyClass(MyLib lib) {
    return castFrom(new1(lib));
  }

So you can write:

final c = MyClass(lib);

instead of:

final c = MyClass.new1(lib);

brianquinlan avatar Apr 15 '22 23:04 brianquinlan

That would be a good start. I was also thinking of translating every method starting with "init" into a constructor.

liamappelbe avatar Apr 15 '22 23:04 liamappelbe

Decided to just translate Foo.new1() into Foo(), as Brian suggests. Translating all init methods too would be a bit confusing for users, wouldn't improve ergonomics that much, and might cause a lot more name collisions.

liamappelbe avatar Feb 26 '25 03:02 liamappelbe