Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to Specify @MainActor for a closure with empty parameters #76539

Open
omochi opened this issue Sep 18, 2024 · 2 comments
Open

Unable to Specify @MainActor for a closure with empty parameters #76539

omochi opened this issue Sep 18, 2024 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels

Comments

@omochi
Copy link
Contributor

omochi commented Sep 18, 2024

Description

Unable to specify @MainActor for a closure with explicit empty parameters

Reproduction

The following code does not compile:

Task { @MainActor () in
}
// error: Extraneous whitespace between attribute name and '('

Expected behavior

This compiles.

Environment

$ swift --version
swift-driver version: 1.115 Apple Swift version 6.0 (swiftlang-6.0.0.9.10 clang-1600.0.26.2)
Target: arm64-apple-macosx15.0

Additional information

No response

@omochi omochi added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Sep 18, 2024
@HeMet
Copy link

HeMet commented Sep 18, 2024

It works without parenthesis

Task { @MainActor in }

@omochi
Copy link
Contributor Author

omochi commented Sep 19, 2024

Thank you for the information.
I’m aware that this case can be written in the way you described.

However, that approach relies on type inference for function parameters, which is different from what I’m trying to report.
The issue I’m raising is that it’s not possible to explicitly specify the function parameters.

For example, in the following code, the two approaches have different meanings:

let parameterInferred = { @MainActor in
    print($0, $1)
}

let parameterExplicit = { @MainActor() () in
    print($0, $1)
}

In parameterInferred, since the parameters are not explicitly specified, the types are inferred from the implementation, and the result is a function with two parameters.

In parameterExplicit, because the parameters are explicitly specified, this results in a compile-time error. I have added two sets of parentheses as a workaround for this bug.

As these two approaches have different meanings, it is inconvenient that the latter form cannot be used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels
Projects
None yet
Development

No branches or pull requests

2 participants