Proposal: Support conditionally compiled endpoints in @DependencyClient
#458
Closed
fruitcoder
started this conversation in
Ideas
Replies: 1 comment 2 replies
|
Hi @fruitcoder, the way you have proposed things it only works if all of the For example, this client: @DependencyClient
struct Client {
#if os(iOS) || os(tvOS)
var fetch1: () -> Void
#endif
#if os(iOS)
var fetch2: () -> Void
#endif
}…would not produce an initializer that allows setting There is no way for us to analyze the |
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Proposal: Support conditionally compiled endpoints in
@DependencyClientI'd like to propose adding support for endpoints declared inside
#if/#endifblocks in@DependencyClient.Motivation
Currently,
@DependencyClientonly discovers endpoints declared directly in the type body. Endpoints inside conditional compilation blocks are silently ignored, meaning they don't receive the same treatment as unconditional endpoints:@DependencyEndpointisn't applied.For example:
Today,
fetchiOSis effectively invisible to the macro.Proposed approach
The macro would treat declarations inside conditional compilation blocks the same way it treats unconditional members.
During expansion, each
#ifbranch is processed independently and grouped by its compilation condition. These groups are then used during initializer generation.The generated initializers follow a few rules:
#if-guarded initializer that includes both the unconditional endpoints and the endpoints for that condition.#if, ensuring Swift's definite initialization rules are satisfied.For the example above, this results in:
Keeping behavior consistent
Rather than generating the unimplemented closures separately, the implementation reuses the existing
DependencyEndpointMacroexpansion and extracts the generated initializer from its synthesized peer declaration.This means the fallback implementation always stays consistent with
@DependencyEndpointitself, including future changes to its generated code and the dynamic\(Self.self)type name used in issue reporting.Why this approach?
The primary goal is to make conditional endpoints behave exactly like unconditional ones while preserving Swift's compilation and initialization semantics.
From a user's perspective, platform-specific endpoints become a first-class feature of
@DependencyClientinstead of requiring workarounds or producing surprising omissions.I'd appreciate feedback on both the overall direction and the generated initializer strategy. In particular, if there's a simpler or more idiomatic approach to handling conditional initialization that I've overlooked, I'd love to hear thoughts.
Sample Implementation
A PR implements the behavior outlined in this discussion.
All reactions