Hello - I'm a first time poster here so please let me know if I'm in the right category. I posted this question on StackOverflow today and it was suggested I post here for further discussion. See original question here.
However, the following error is seen for the ternary operator for stepBackward (and stepForward): "Type of expression is ambiguous without a type annotation."
This is a clean Playgrounds workspace with the default Hello World removed to verify the behavior.
I'm able to fix this by writing explicit type annotation as follows:
let forwardFunc: (Int) -> Int = stepForward
let backwardFunc: (Int) -> Int = stepBackward
@MainActor func chooseStepFunction(backward: Bool) -> (Int) -> Int {
return backward ? backwardFunc : forwardFunc
}
This is to my understanding a good practice, but the documentation in the Functions section does not make clear this would be required.
Did something change in Swift recently that would make the documentation out of date? Is writing an explicit type annotation in these cases required now? I would like to understand why in this context the type annotation is now required, in particular with this ternary operator using inferred function types.
That code compiles error free on my machine using:
No imports
Swift 5.9 (using Swift 6 compiler)
Swift version 6.0.3 (swift-6.0.3-RELEASE)
Target: x86_64-unknown-linux-gnu
Arch Linux (6.12.10-arch1-1)
VS Code (1.96.2)
I also plugged it into Swift Fiddle (which runs on Linux) running Swift 6.0.2, 5.9 and 2.2 with no errors.
I even use this pattern in my production projects (running on a Linux server) with no problems.
That code also compiles error free on my intel iMac with Xcode 16.2, Swift 6.0.3, building for iOS 17.5, with and without importing UIKit.
My best guess is a Swift Playgrounds bug or outdated cache somewhere. I've never used Swift Playgrounds so I can't really give you any solutions that usually work with Xcode or VS Code (like cleaning the build or closing and reopening the project).
Even your original example works okay outside of playgrounds for me (whoops, started writing before RandomHashTags posted), so I'm going to guess it's something specific to the processing that happens for interactivity there. That could also explain the poor message, if the problem is being caused by injected code and therefore "proper" diagnostics can't find anything to attach to. Still, worth filing an issue report about: either this is expected behavior for playgrounds and the compiler should give a better diagnostic (and the docs updated to mention this, probably), or it's not expected behavior and they can fix it.
Very strange. I am still on 16B40, and it works there in a fresh package (as well as on the command line with a direct swiftc invocation where I originally tested). Maybe there's been a regression, or maybe it depends on something else being imported. Regardless, someone should look into it!
Instructions on how to report an issue are available on the main site under Contributing: Swift.org - Contributing
I think the problem here is stepForward and stepBackward are implicitly @Sendable. Type checker failed to cast from @Sendable (Int)->Int to (Int) -> Int.
Can confirm, Swift 6 language mode breaks it and adding @Sendable to the return type fixes it. This is definitely worthy of a GitHub issue for swiftlang/swift.