Skip to content

Conversation

coenttb
Copy link
Contributor

@coenttb coenttb commented Jul 27, 2025

Summary

  • Adds support for using throwing closures with .convert conversions
  • Uses @_disfavoredOverload to maintain backward compatibility
  • Implements direct storage of throwing closures for optimal performance
  • Includes comprehensive tests and detailed documentation

Changes

  • Extended .convert API: Added overload that accepts throwing apply and unapply closures
  • New throwing initializer: AnyConversion.init(apply: @escaping (Input) throws -> Output, unapply: @escaping (Output) throws -> Input)
  • Comprehensive tests: Added ConvertTests.swift with coverage for throwing/non-throwing scenarios and overload disambiguation
  • Detailed documentation: Complete DocC documentation with usage examples and performance guidance
  • Backward compatibility: Existing non-throwing .convert usage remains unchanged

Test Plan

  • All existing tests pass (257 tests total)
  • New tests verify throwing convert functionality works correctly
  • Non-throwing convert still works as expected
  • Throwing closures properly propagate custom errors
  • Overload disambiguation ensures compiler selects correct variant
  • Build succeeds without warnings

Implementation Details

The solution uses @_disfavoredOverload on the throwing overload to resolve compiler ambiguity, ensuring the non-throwing version is preferred when both signatures are compatible. The throwing initializer directly stores the throwing closures without additional wrapping, maximizing performance while providing the flexibility to throw custom errors instead of generic ConvertingError.

This complements the existing optional-based conversion by allowing developers to throw specific, meaningful errors when conversion logic fails, improving error handling and debugging experience.

- Add @_disfavoredOverload convert overload for throwing closures
- Implement throwing initializer in AnyConversion with direct closure storage
- Add comprehensive tests covering throwing and non-throwing scenarios
- Maintain backward compatibility and overload disambiguation
- Include detailed documentation with usage examples
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant