On Windows, sleep
, URLRequest
, and URLSession
from Foundation are not known (Swift 5.5.2; error occurring with import Foundation
, no other import). So is Foundation still incomplete on Windows? If yes, is there a chance that this will be implemented soon, and how can I know that something is not supported on a certain platform without trying?
I would split your question into two parts.
URLRequest
, URLSession
and other types related to network requests are placed in FoundationNetworking
instead of Foundation
on non-Darwin platforms. This helps to strip libcurl
dependency from Foundation
because it’s heavy and less likely used.
sleep
is not an interface of Foundation
. It is exposed as a part of the C standard library (Darwin.C
on Apple systems or Glibc
on Linux/BSD), which is imported by Foundation
. However, ucrt
on Windows doesn’t expose sleep
, that’s why you can’t find it by importing Foundation
.
In fact, such exposure of APIs from an imported third-party library is not ideal. Some community members are working on import access control, which will introduce hiding external APIs by default, see [Pre-Pitch] Import access control: a modest proposal. If you want to use sleep
, you’d better import Darwin.C
or Glibc
explicitly.
The open-source, Swift-based implementation of Foundation
is available and under development at GitHub - apple/swift-corelibs-foundation: The Foundation Project, providing core utilities, internationalization, and OS independence. All toolchains available at Swift.org uses the open-source implementation, so you can easily check them out.
@stevapple answered the specifics of your issue. To answer the general question, yes, Foundation is still incomplete on non-Apple platforms. My experience is mainly limited to URLSession
, but it still has limited support for the full set of features we have on Apple platforms, like customized redirects and caching. The simpler parts generally work though.
Thank you both for your answers.
Not ideal to have such a difference. Should then be the same on macOS.
Q: How can I then "sleep" on Windows?
Hmm. swift.org points to Swift Standard Library | Apple Developer Documentation for documentation, which is only for Apple platforms, maybe this will be better at some point.
Foundation is still incomplete on non-Apple platforms.
It it will be complete at some point, OK. If not, the compiler should be able to warn when using parts of the Foundation & Co. that are not implemented on all platforms (but I have no idea how this should work). At least the documentation should be accordingly.
It generally warns when a feature is completely unavailable, but abilities within a feature do not, like the URLSession
delegate methods which may not be called or behave differently.
Q: How can I then "sleep" on Windows?
The portable spelling is Thread.sleep(forTimeInterval:)
.