Repository navigation
Conversation
Import FoundationEssentials instead of Foundation where the toolchain provides it, and fall back to Foundation elsewhere. The Date and DateComponents support is guarded by either module being available.
|
@dduan please do let me know your thoughts on this. Thanks |
|
hey @nmcc24 thanks! I love the idea. But looks like the following would cash on Linux: import FoundationEssentials
import TOMLDecoder
struct Value: Decodable {
let datetime: Date
}
var decoder = TOMLDecoder()
decoder.strategy.date = .calendar(identifiedBy: .islamic)
let value = try decoder.decode(
Value.self,
from: "datetime = 2021-01-01T00:00:00Z"
)
print(value.datetime.timeIntervalSince1970) |
@dduan Thanks for catching this, and sorry for missing it. FoundationEssentials only provides the Gregorian and ISO 8601 calendars. For any other identifier, Making Essentials the default would change behaviour for existing users, so I'd suggest an opt-in instead: a default-enabled package trait that keeps |
|
A breaking change at runtime for existing users would not be possible, unfortunately. |
@dduan Agreed, and to be clear, I'm not proposing that anymore. The current diff still makes FoundationEssentials the default, which is exactly the runtime break you found. I'll rework it so existing users see no change at all:
Would you be open to that? If so, I'll update the PR. If not, no problem, I'll close it. |
Summary
FoundationEssentialsinstead ofFoundationwhere the toolchain provides it, and fall back toFoundationelsewhere.public import FoundationinTOMLDecoder.swiftandDateTime.swiftstays public in both branches.DateandDateComponentssupport withcanImport(FoundationEssentials) || canImport(Foundation), so it's available with either module.TOMLSingleValueDecodingContainer.swift.gybalong with its generated file.Impact
On Apple platforms nothing changes:
FoundationEssentialsisn't importable there, so the#elsebranch importsFoundationas before.On Linux, Windows, Android and WASI,
Foundationis swift-corelibs-foundation. It re-exportsFoundationEssentials, and also brings inFoundationInternationalization, CoreFoundation and ICU (_FoundationICU). Because TOMLDecoder usespublic import Foundation, every package that depends on it gets fullFoundationimported and linked, even when that package only needsFoundationEssentials.With this change, such a package can:
libFoundation.so,libFoundationInternationalization.soandlib_FoundationICU.so. For a minimal program on Linux with Swift 6.3, the Foundation libraries it loads drop from about 60 MB to about 9 MB, and its peak memory from about 16 MB to about 10 MB;--static-swift-stdlib, the static Linux SDK), keep fullFoundationoff the link line, so CoreFoundation and ICU's data can't be pulled in;TOMLDecoder only needs
Data,Date,Calendar,TimeZone,DateComponentsandDecimal, which are all inFoundationEssentials. swift-argument-parser already importsFoundationEssentialsthe same way.Validation
swift test(751 tests) on Linux (Swift 6.3) and macOS (Xcode 27.2)Scripts/test-generate.shScripts/format.sh --lint .