Repository navigation
Description
Background
Several Apple frameworks expose failable Objective-C initializers — init* methods that can return nil on failure. In C# we expose these as static nullable factory methods (e.g. Create) rather than constructors, since C# constructors can't return null.
Currently, every such case requires manually writing the same boilerplate in a partial class file:
// src/FSKit/FSEntityIdentifier.cs
[SupportedOSPlatform ("macos27.0")]
public static FSEntityIdentifier? Create (NSUuid uuid, NSData qualifierData)
{
if (uuid is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (uuid));
if (qualifierData is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (qualifierData));
var rv = new FSEntityIdentifier (NSObjectFlag.Empty);
rv.InitializeHandle (rv._InitWithUuidQualifierData (uuid, qualifierData), "initWithUUID:qualifierData:", false);
if (rv.Handle == NativeHandle.Zero) {
rv.Dispose ();
return null;
}
return rv;
}This same pattern appears in at least:
src/FSKit/FSEntityIdentifier.cssrc/WebKit/WKWebExtensionMatchPattern.cssrc/AVKit/AVCaptureEventSound.cssrc/SensitiveContentAnalysis/SCVideoStreamAnalyzer.cssrc/ScreenTime/STWebHistory.cssrc/AudioUnit/AUHeadTrackingBinauralRenderer.cssrc/Foundation/NSAttributedString.cssrc/AVFoundation/AVAudioRecorder.cssrc/AVFoundation/AVAudioPlayer.cs
Proposal
Add a bgen attribute (or extend an existing one) so that the API definition file can declare this intent, and bgen generates the boilerplate automatically.
Option A — [FactoryMethod] attribute on the binding constructor:
[Export ("initWithUUID:qualifierData:")]
[FactoryMethod ("Create")]
NativeHandle Constructor (NSUuid uuid, NSData qualifierData);bgen would:
- Suppress generation of the public C# constructor for that selector.
- Instead generate a
public static T? Create (...)method with the same parameters plus null-guard checks, callingInitializeHandle (..., allowNullHandle: false)and returningnullwhen the handle is zero.
Option B — Rename the binding method from Constructor to match the desired factory name:
[Export ("initWithUUID:qualifierData:")]
NativeHandle Create (NSUuid uuid, NSData qualifierData);bgen could detect that a method named differently from Constructor but whose selector starts with init is a failable initializer, and generate the static nullable factory.
Open questions:
- Should the attribute carry the factory method name, or should bgen derive it from the managed method name?
- Is there an existing attribute (
[Constructor]) we could reuse or extend? - Not all
init*selectors are failable — we need a way to distinguish which ones can return nil. The attribute approach is more explicit. - Should the generated factory method respect platform availability attributes (e.g.
[SupportedOSPlatform])?
References
- Introduced in: [FSKit] Update bindings up to Xcode 27.0 Beta 3 #26163 (FSEntityIdentifier)
- Discussion: [FSKit] Update bindings up to Xcode 27.0 Beta 3 #26163 (comment)
Activity
rolfbjarne commentedon Jul 17, 2026
This is interesting, the original manual code that made me ask Copilot to file this issue was not because it was a failable ObjC initializer, but because there was already another C# constructor with the same managed signature (and thus a factory method was needed). Somehow Copilot reasoned incorrectly and came up with the other correct answer.
Anyways, both reasons are valid reasons for needing a factory method.
FWIW I'm leaning towards option A (
[FactoryMethod]) - being explicit makes the api definition easier to read.dalexsoto commentedon Jul 17, 2026
@rolfbjarne yeah agreed +1 on the option A
[FactoryMethod]I like that it is explicit[bgen] Add [FactoryMethod] support for failable initializers. Fixes #…