- Notifications
You must be signed in to change notification settings - Fork 10.6k
Description
Description
A default expression for type inference of a protocol when another parameter is an associated type of the protocol works for static methods but not functions.
Consider Task.sleep:
static func sleep<C>( until deadline: C.Instant, tolerance: C.Instant.Duration? = nil, clock: C = ContinuousClock() ) async throws where C : ClockThis method has a generic type C constrained to Clock, and it's parameters use both the types C and C.Instant. C is inferred from the clock parameter's the default expression ContinousClock().
Task.sleep is inexpressible as a function, even though no type information from Task is required—Task is strictly used as a namespace. As far as I know, static methods should be expressible as functions as long as they don't depend on anything from the type.
Steps to reproduce
Here's a minimal example using a default expression for inference of a protocol in a static method and as a function, with the function failing to compile:
protocol Parent { associatedtype Child } struct Default: Parent { struct Child {} } enum Namespace { static func defaulting<P: Parent>( child: P.Child, parent: P = Default() ) {} } func defaulting<P: Parent>( child: P.Child, parent: P = Default() ) {}The function defaulting fails to compile with the message, Cannot use default expression for inference of 'P' because it is inferrable from parameters #0, #1.
Expected behavior
I'd expect to be able to express functions the same as static methods such as Task.sleep and, from the example above, Namespace.defaulting.
Environment
- Swift compiler version info
swift-driver version: 1.75.2 Apple Swift version 5.8.1 (swiftlang-5.8.0.124.5 clang-1403.0.22.11.100)
Target: arm64-apple-macosx13.0 - Xcode version info Xcode 14.3.1 (14E300c)
- Deployment target: N/A, any