/claim #518 Ready for review if there is something wronk pls let me know so i can try to fix it!
🚀 Feature: Into/As Type Classes (Macro Derivation) - 100% Complete
Status: ✅ Feature Complete (100% Implementable Requirements Met)
This PR implements automatic derivation for Into[A, B] and As[A, B] type classes using Scala 3 Macros (Quotes). The implementation provides type-safe, compile-time derivation for complex data transformations without runtime reflection, ensuring full compatibility with Scala.js and Scala Native.
✨ Key Features
1. Collections & Maps (Complete Support)
- Standard Collections:
List ↔ Vector ↔ Set ↔ Seq ↔ Array conversions.
- Map Support (NEW): Full support for
Map[K1, V1] -> Map[K2, V2].
- Key Conversion: Automatically derives converters for keys (e.g.,
Map[Int, V] -> Map[Long, V]).
- Value Conversion: Recursively converts values (e.g.,
Map[K, List[Int]] -> Map[K, Vector[Long]]).
- Deep Nesting: Handles arbitrarily nested structures inside Maps.
- Element Coercion: Recursively transforms elements across all collection types.
2. Product Types (Case Classes & Tuples)
- Smart Field Mapping: Maps fields by Name, Type, or Position.
- Dual Compatibility Strategy:
- Strict Mode (Priority 3): For “Unique Type” matching, enforces strict type safety (separates Integrals from Fractionals).
- Loose Mode (Priority 4): For “Positional” matching, allows broader numeric compatibility (e.g.,
Int -> Double) as position disambiguates intent.
- Tuple Support: Converts between Case Classes and Tuples, and Tuple-to-Tuple (generic arity).
3. Coproduct Types (Sealed Traits & Enums)
- Enum Conversion: Converts Scala 3 Enums and Sealed Traits based on subtype names.
- Singleton Support: Correctly handles
case object and Enum values (0-arity) using TermRef matching.
- Pattern Matching: Generates optimized
match expressions with Bind patterns for recursion.
4. Validation & ZIO Prelude
- Opaque Types: Detects and uses companion object
apply methods for validation.
- ZIO Prelude Newtypes (NEW): Full support for
Newtype and Subtype derivation, automatically wiring compile-time validation rules into the derivation logic.
- Fail-Fast: Returns
Left(SchemaError) for validation failures (e.g., numeric overflow, constraints).
5. Bidirectional As[A, B]
- Derived automatically as a composition of
Into[A, B] and Into[B, A].
- Supports Round-Trip validation checks.
🛠️ Technical Implementation Details
- Macro Strategy: Uses
Mirror.ProductOf via compile-time Quotes (Zero Runtime Reflection).
- Zero Experimental Features: Strictly compliant with stable Scala 3.3.x APIs (no
@experimental).
- Cross-Platform: Verified on JVM, JS, and Native.
- Map Extraction: Custom logic (
extractMapTypes, deriveMapInto) to decompose Map[K, V] types and derive converters for both type parameters.
- Recursion Handling: Implements “If-Else Cascade” strategy for Coproducts to avoid AST construction crashes.
⚠️ Known Limitations
- Structural Types: Derivation for structural types (e.g.,
{ def name: String }) is not supported due to Scala 3 SIP-44 limitations regarding macro context casting. This is the only feature from the original scope that was deemed technically impossible without unstable flags.
📊 Test Coverage & Statistics