Skip to content

3.0.0

Latest

Choose a tag to compare

@amine2233 amine2233 released this 31 Oct 07:25
fix(result): migrate to Swift stdlib Result and SwiftPM-only build

Replace the custom `Result<Value, Error>` enum with extensions on the
Swift standard library `Result<Success, Failure>` type, and move the
package to a SwiftPM-only layout built with swift-tools-version 6.0.

Why: since Swift 5 the standard library ships its own `Result` type.
The package-local enum shadowed it, which caused ambiguity errors and
conflicting overloads (several helpers relied on force casts such as
`error as! Error`, which could crash at runtime). Building on the
stdlib type removes the ambiguity and the unsafe casts.

Source changes:
- New Sources/Result/Result.swift conforms the stdlib `Result` to
`ResultProtocol` and re-adds the helpers: isSuccess, isFailure,
value, error, description/debugDescription (`@retroactive`),
convenience inits, compactMap, compactMapError, fanout, bimap,
recover, `??` operators and the callback-style flatMap.
- `map`, `flatMap`, `mapError` and `flatMapError` are dropped in
favour of the stdlib implementations.
- `init(block:)` and `tryMap` now only exist where
`Failure == Swift.Error`, removing the `as!` casts.
- `convertThrow()` and `resolve()` now delegate to `get()`.
- ResultProtocol associated types renamed Value -> Success and
Error -> Failure; ErrorConvertible and ResultExtension updated.
- `ResultProtocol.result` no longer force-unwraps `error` when a
value is present.

Build/packaging changes:
- Package.swift bumped to swift-tools-version 6.0 and gains a
ResultTests test target.
- Sources moved to Sources/Result and tests to Tests/ResultTests.
- Removed Result.xcodeproj, shared schemes, Info.plists, Result.h,
Gemfile/Gemfile.lock and both ResultKit podspecs.
- .gitignore now ignores .swiftpm and .build.

Behavior changes / risks:
- Source-breaking: generic parameters are now `Success`/`Failure`;
code referencing `Result.Value` or `Result.Error` must be updated.
- Custom `==`/`!=` removed; equality comes from stdlib `Equatable`
conformance.
- The global `??` operator and `init(_ throwing:)` were removed.
- `init(block:)` is no longer available for typed failures.
- `@retroactive` conformances to Custom(Debug)StringConvertible may
conflict if the stdlib adds its own in the future.
- CocoaPods and Carthage distribution is no longer supported.
- Requires a Swift 6 toolchain to resolve the package.

Things to test:
- `swift build` and `swift test` on macOS and Linux.
- Consumers importing `Result` compile without ambiguity.
- tryMap with both ErrorConvertible and `Swift.Error` failures.
- apply / <*> with a Semigroup failure type.
- recover, `??` and the callback flatMap on both cases.
- `ResultProtocol.result` for success and failure values.