Skip to content

Ambiguous use of 'init(name:priority:operation:)' in onChange(of:_:) modifier #84587

@lukaskollmer

Description

@lukaskollmer

Description

(i tried reproducing this outside of SwiftUI, but was unable to. the example below is the smallest i managed to reduce this issue.)

the compiler is unable to compile the program below, failing in the onChange closure.
i have no idea what is causing this, but either of the following changes makes it compile fine:

  • making S1 non-generic;
  • making the T in S1.init non-optional;
  • making S1.init non-generic and changing the 1st param's type to Int?;
  • simplifying the expression passed to S1's initializer to a non-ternary;
  • extracting the Task creation into a separate instance method, and calling that from the onChange closure

Reproduction

import SwiftUI struct S1<A: View>: View { var body: some View { EmptyView() } init<T>( _: T?, @ViewBuilder _: () -> A = { EmptyView() } ) {} } struct S2: View { let x: Int var body: some View { S1(true ? x : nil) .onChange(of: x) { _, _ in Task {} // Ambiguous use of 'init(name:priority:operation:)' } } }

Expected behavior

it should work

Environment

swift-driver version: 1.127.14.1 Apple Swift version 6.2 (swiftlang-6.2.0.19.9 clang-1700.3.19.1)
Target: arm64-apple-macosx15.0

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.triage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions