Public initializer ergonomics for Draft types after 33.0 #308
Replies: 2 comments
|
@woodymelling This came up in an issue recently: #309 I'll restate things here: We don't think it should be a macro's job to manage the complexities of memberwise initializer generation, capturing all the nuance of the Swift compiler, and it also seems to go against the spirit of the language, which requires you to explicitly add public surface area like those memberwise initializers. Unfortunately the big footgun here is that if you try to define the equivalent memberwise initializer in an extension you will get hit with the following error: extension MyTable.Draft {
public init(…) { … }
}
There are workarounds, though:
The first is probably the most straightforward solution with the fewest compromises. I also opened an issue in the language here if you'd like to follow it: swiftlang/swift#90519 It does seem like this is a bug that could be fixed in the language with same-file extension support. |
|
We've pitched a fix in the language for this issue here: https://forums.swift.org/t/same-file-memberwise-initializer-extensions/88466 |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
After upgrading past 0.32, I’m running into a source-breaking issue where I can no longer initialize
Draftvalues from outside the target where the@Tabletype is defined.From Slack, I understand that making the macro-generated
Draftmemberwise initializer internal was intentional, since relying on that compiler behavior was not something StructuredQueries should have committed to publicly supporting. That makes sense.The awkward part is that I also seem unable to layer on the exact public initializer myself in some cases. In my app this means I cannot expose the same ergonomic construction surface for domain-heavy draft models from another target.
A rough shape of the problem:
This worked on 0.32, but no longer compiles after the initializer visibility change.
I tried adding an explicit public initializer for the draft type, but hit a compiler error when matching the generated draft shape. I can put together a minimal repro, but wanted to first ask:
Is the recommended path now to manually define public initializers on Draft types when they need to cross module boundaries?
Is there an expected pattern for doing that when the draft type has generated/lazy/optionalized fields?
I’m not asking for the generated memberwise initializer to become public again if that was an intentional correction. I’m more looking for the blessed way to recover a public construction API for drafts in multi-target apps.
All reactions