You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -23,9 +23,13 @@ ZIO Blocks extends `Schema[A]` to support structural types through:
23
23
| Feature | Description |
24
24
|---------|-------------|
25
25
|`.structural`| Convert any product schema to its structural equivalent |
26
-
|`Schema.derived[StructuralType]`| Derive schemas directly for structural types |
26
+
|`Schema.derived[StructuralType]`| Derive schemas directly for structural types (JVM only) |
27
27
| Normalized type names | Deterministic, comparable type identifiers |
28
28
29
+
:::caution JVM Only
30
+
Structural type support requires reflection and is only available on the JVM. Attempting to use structural types on Scala.js or Scala Native will result in a compile-time error.
31
+
:::
32
+
29
33
## Converting to Structural Schemas
30
34
31
35
Any schema for a product type (case class, tuple) can be converted to its structural representation:
@@ -42,10 +46,6 @@ val structuralSchema = nominalSchema.structural
42
46
// Note: fields are alphabetically ordered in the structural type
43
47
```
44
48
45
-
:::note JVM Requirement
46
-
The `.structural` method requires JVM because it uses reflection to create the structural type at runtime. For cross-platform code, derive schemas for Selectable/Dynamic types directly instead.
47
-
:::
48
-
49
49
### Type Name Normalization
50
50
51
51
Structural type names are normalized for consistent comparison:
@@ -101,84 +101,24 @@ val structural = Schema.derived[Container].structural
101
101
102
102
## Deriving Schemas for Structural Types
103
103
104
-
You can derive schemas directly for structural types without starting from a nominal type.
### Pure Structural Types (Scala 2: All Platforms, Scala 3: JVM Only)
155
-
156
-
In Scala 2, you can derive schemas for pure structural types on **all platforms** because the macro generates an anonymous Dynamic class at compile time:
104
+
You can derive schemas directly for structural types. This feature requires JVM because it uses reflection for deconstruction.
157
105
158
106
```scala
159
-
//Scala 2 - Works on JVM, JS, and Native!
107
+
//JVM only - uses reflection
160
108
typePersonLike= { defname:String; defage:Int }
161
109
valschema=Schema.derived[PersonLike]
162
-
163
-
// The macro generates an anonymous Dynamic class, no runtime reflection needed
164
110
```
165
111
166
-
In Scala 3, pure structural types without a `Selectable` base only work on JVM:
112
+
:::caution Platform Restriction
113
+
Structural type derivation requires reflection and only works on the JVM. Attempting to derive a schema for a structural type on JS or Native will result in a compile-time error:
167
114
168
-
```scala
169
-
// Scala 3 JVM only - uses reflection
170
-
typePointLike= { defx:Int; defy:Int }
171
-
valschema=Schema.derived[PointLike] // Only works on JVM
172
115
```
116
+
Cannot derive Schema for structural type '...' on JS/Native.
173
117
174
-
:::caution Platform Restriction
175
-
In Scala 3, pure structural types (without `Selectable`) require reflection and only work on the JVM. Attempting to derive a schema for a pure structural type on JS or Native will result in a compile-time error.
176
-
177
-
In Scala 2, pure structural types work on all platforms because the macro generates Dynamic classes at compile time.
178
-
:::
118
+
Structural types require reflection which is only available on JVM.
179
119
180
-
:::note Converting Nominal to Structural
181
-
The `.structural` method on `Schema` (for converting `Schema[CaseClass]` to its structural equivalent) requires JVM on **both** Scala 2 and Scala 3 because it uses reflection to create the structural type.
120
+
Consider using a case class instead.
121
+
```
182
122
:::
183
123
184
124
## Sum Types and Union Types
@@ -219,30 +159,7 @@ val schema = Schema.derived[Status]
219
159
220
160
### Scala 2: Sum Type Limitation
221
161
222
-
In Scala 2, sealed traits cannot be converted to structural types because Scala 2 lacks union types. Attempting to call `.structural` on a sealed trait schema will produce a compile-time error:
Cannot convert sum type 'Status' to structural type.
237
-
238
-
Sum types (sealed traits, enums) cannot be represented as structural types in Scala 2
239
-
because Scala 2 does not support union types.
240
-
241
-
Consider:
242
-
- Using a case class wrapper instead of a sealed trait
243
-
- Using the nominal schema directly
244
-
- Upgrading to Scala 3 for union type support
245
-
```
162
+
In Scala 2, sealed traits cannot be converted to structural types because Scala 2 lacks union types. Attempting to call `.structural` on a sealed trait schema will produce a compile-time error.
246
163
247
164
## Platform Compatibility
248
165
@@ -254,9 +171,7 @@ Consider:
254
171
| Large Products (>22 fields) | ✅ | ✅ | ✅ | ✅ |
255
172
| Sealed Trait `.structural`| ❌ (no unions) | ❌ (no unions) | ✅ | ❌ (needs reflection) |
0 commit comments