@zio.dev/zio-blocks 0.0.33 → 0.0.51
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/guides/compile-time-resource-safety-with-scope.md +16 -17
- package/guides/getting-started-with-mux.md +1507 -0
- package/guides/query-dsl-extending.md +161 -102
- package/guides/query-dsl-fluent-builder.md +217 -157
- package/guides/query-dsl-reified-optics.md +12 -10
- package/guides/query-dsl-sql.md +246 -165
- package/guides/telemetry-guide.md +1069 -0
- package/guides/zio-schema-migration.md +29 -22
- package/index.md +292 -50
- package/package.json +1 -1
- package/plans/config-follow-up-prs.md +188 -0
- package/plans/config-pr-assessment-roadmap.md +310 -0
- package/reference/MuxDataFlow.jsx +250 -0
- package/reference/async.md +651 -0
- package/reference/chunk.md +3533 -308
- package/reference/codegen/case-class.md +436 -0
- package/reference/codegen/emitter-config.md +383 -0
- package/reference/codegen/examples.md +664 -0
- package/reference/codegen/field.md +316 -0
- package/reference/codegen/index.md +317 -0
- package/reference/codegen/scala-emitter.md +392 -0
- package/reference/codegen/scala-file.md +276 -0
- package/reference/codegen/sealed-trait.md +408 -0
- package/reference/codegen/type-definition.md +340 -0
- package/reference/codegen/type-ref.md +201 -0
- package/reference/combinators.md +347 -117
- package/reference/config.md +158 -0
- package/reference/context.md +4 -4
- package/reference/datastar.md +346 -0
- package/reference/docs.md +1461 -345
- package/reference/endpoint/auth-type.md +146 -0
- package/reference/endpoint/endpoint.md +297 -0
- package/reference/endpoint/http-codec.md +249 -0
- package/reference/endpoint/index.md +825 -0
- package/reference/endpoint/path-codec.md +237 -0
- package/reference/endpoint/route-pattern.md +196 -0
- package/reference/endpoint/route-tree.md +111 -0
- package/reference/endpoint/segment-codec.md +212 -0
- package/reference/html.md +1120 -0
- package/reference/htmx/attribute-values.md +359 -0
- package/reference/htmx/hx-encoding.md +111 -0
- package/reference/htmx/hx-params.md +204 -0
- package/reference/htmx/hx-swap.md +276 -0
- package/reference/htmx/hx-sync.md +251 -0
- package/reference/htmx/hx-target.md +314 -0
- package/reference/htmx/hx-trigger.md +457 -0
- package/reference/htmx/hx-url-update.md +239 -0
- package/reference/htmx/index.md +855 -0
- package/reference/http-model/index.md +47 -0
- package/reference/http-model/model.md +1481 -0
- package/reference/http-model/schema.md +747 -0
- package/reference/maybe.md +826 -0
- package/reference/media-type.md +2 -2
- package/reference/mux.mdx +823 -0
- package/reference/openapi.md +1351 -0
- package/reference/resource-management/defer-handle.md +1 -1
- package/reference/resource-management/resource.md +31 -2
- package/reference/resource-management/scope.md +28 -12
- package/reference/resource-management/wire.md +3 -7
- package/reference/ringbuffer/MpmcDiagram.jsx +717 -0
- package/reference/ringbuffer/MpscDiagram.jsx +618 -0
- package/reference/ringbuffer/SpmcDiagram.jsx +680 -0
- package/reference/ringbuffer/SpscDiagram.jsx +677 -0
- package/reference/ringbuffer/advanced.mdx +109 -0
- package/reference/ringbuffer/index.mdx +145 -0
- package/reference/ringbuffer/mpmc.mdx +151 -0
- package/reference/ringbuffer/mpsc.mdx +132 -0
- package/reference/ringbuffer/spmc.mdx +108 -0
- package/reference/ringbuffer/spsc.mdx +344 -0
- package/reference/{allows.md → schema/allows.md} +4 -4
- package/reference/{binding-resolver.md → schema/binding-resolver.md} +1 -1
- package/reference/{binding.md → schema/binding.md} +2 -3
- package/reference/schema/built-in-codecs/avro.md +451 -0
- package/reference/schema/built-in-codecs/bson.md +480 -0
- package/reference/schema/built-in-codecs/csv.md +564 -0
- package/reference/schema/built-in-codecs/index.md +77 -0
- package/reference/schema/built-in-codecs/json/index.md +295 -0
- package/reference/schema/built-in-codecs/json/json-config.md +217 -0
- package/reference/{json-patch.md → schema/built-in-codecs/json/json-patch.md} +5 -5
- package/reference/{json-schema.md → schema/built-in-codecs/json/json-schema.md} +14 -47
- package/reference/schema/built-in-codecs/json/json-selection.md +322 -0
- package/reference/{json.md → schema/built-in-codecs/json/json.md} +32 -64
- package/reference/schema/built-in-codecs/messagepack.md +508 -0
- package/reference/schema/built-in-codecs/thrift.md +433 -0
- package/reference/schema/built-in-codecs/toon.md +1078 -0
- package/reference/{xml.md → schema/built-in-codecs/xml.md} +13 -9
- package/reference/schema/built-in-codecs/yaml.md +552 -0
- package/reference/{codec.md → schema/codec.md} +10 -10
- package/reference/{dynamic-optic.md → schema/dynamic-optic.md} +151 -5
- package/reference/{dynamic-schema.md → schema/dynamic-schema.md} +8 -8
- package/reference/schema/format.md +92 -0
- package/reference/schema/index.md +50 -0
- package/reference/schema/migration.md +297 -0
- package/reference/{modifier.md → schema/modifier.md} +58 -7
- package/reference/{optics.md → schema/optics.md} +2 -2
- package/reference/{patch.md → schema/patch.md} +1 -1
- package/{path-interpolator.md → reference/schema/path-interpolator.md} +165 -72
- package/reference/{schema-evolution → schema/schema-evolution}/as.md +8 -8
- package/reference/{schema-evolution → schema/schema-evolution}/index.md +2 -2
- package/reference/{schema-evolution → schema/schema-evolution}/into.md +8 -8
- package/reference/{schema-expr.md → schema/schema-expr.md} +110 -175
- package/reference/{schema.md → schema/schema.md} +12 -0
- package/reference/{structural-types.md → schema/structural-types.md} +1 -1
- package/reference/{type-class-derivation.md → schema/type-class-derivation.md} +63 -1
- package/reference/smithy.md +533 -0
- package/reference/sql/db-codec-deriver.md +71 -0
- package/reference/sql/db-codec.md +687 -0
- package/reference/sql/db-con.md +271 -0
- package/reference/sql/db-connection.md +153 -0
- package/reference/sql/db-param-writer.md +77 -0
- package/reference/sql/db-param.md +66 -0
- package/reference/sql/db-result-reader.md +146 -0
- package/reference/sql/db-tx.md +82 -0
- package/reference/sql/db-value.md +41 -0
- package/reference/sql/ddl.md +85 -0
- package/reference/sql/frag.md +254 -0
- package/reference/sql/index.md +341 -0
- package/reference/sql/repo.md +600 -0
- package/reference/sql/sql-dialect.md +73 -0
- package/reference/sql/sql-logger.md +62 -0
- package/reference/sql/sql-name-mapper.md +70 -0
- package/reference/sql/table-metadata.md +134 -0
- package/reference/sql/table.md +448 -0
- package/reference/sql/transactor-zio.md +399 -0
- package/reference/sql/transactor.md +353 -0
- package/reference/sql-zio.md +112 -0
- package/reference/streams/concurrent-operators.md +106 -0
- package/reference/streams/index.md +653 -0
- package/reference/streams/pipeline.md +718 -0
- package/reference/streams/reader.md +1284 -0
- package/reference/streams/scala-2-compatibility.md +55 -0
- package/reference/streams/sink.md +1426 -0
- package/reference/streams/stream.md +2526 -0
- package/reference/streams/writer.md +1045 -0
- package/reference/streams/zero-boxing.md +275 -0
- package/reference/telemetry.md +693 -0
- package/reference/typeid.md +5 -19
- package/sidebars.js +238 -43
- package/reference/formats.md +0 -694
- package/reference/http-model.md +0 -1716
- package/reference/streams.md +0 -989
- package/ringbuffer.md +0 -249
- /package/reference/{json-differ.md → schema/built-in-codecs/json/json-differ.md} +0 -0
- /package/reference/{dynamic-value.md → schema/dynamic-value.md} +0 -0
- /package/reference/{lazy.md → schema/lazy.md} +0 -0
- /package/reference/{reflect.md → schema/reflect.md} +0 -0
- /package/reference/{registers.md → schema/registers.md} +0 -0
- /package/reference/{schema-error.md → schema/schema-error.md} +0 -0
- /package/reference/{syntax.md → schema/syntax.md} +0 -0
- /package/reference/{validation.md → schema/validation.md} +0 -0
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: type-definition
|
|
3
|
+
title: "TypeDefinition"
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
`TypeDefinition` is a sealed trait that represents any Scala type definition—case classes, sealed traits, enums, objects, newtypes, type aliases, abstract classes, and regular traits. It's the foundation for modeling Scala's type system in the IR.
|
|
7
|
+
|
|
8
|
+
## Use Cases
|
|
9
|
+
|
|
10
|
+
- As the common interface for all compound types when building a `ScalaFile`
|
|
11
|
+
- When you need to handle multiple type kinds uniformly (e.g., iterate over all types in a file)
|
|
12
|
+
- As a base for specific type definitions like `CaseClass`, `SealedTrait`, `Enum`, etc.
|
|
13
|
+
|
|
14
|
+
## Variants
|
|
15
|
+
|
|
16
|
+
`TypeDefinition` has 9 concrete implementations:
|
|
17
|
+
|
|
18
|
+
| Type | Purpose | Scala Feature |
|
|
19
|
+
|-----------------|-----------------------------|--------------------------------------------|
|
|
20
|
+
| `CaseClass` | Product types with fields | Scala 2 & 3 |
|
|
21
|
+
| `SealedTrait` | Sum types (ADTs) with cases | Scala 2 & 3 |
|
|
22
|
+
| `Trait` | Open trait definitions | Scala 2 & 3 |
|
|
23
|
+
| `AbstractClass` | Abstract class definitions | Scala 2 & 3 |
|
|
24
|
+
| `Enum` | Scala 3 enum definitions | Scala 3 only |
|
|
25
|
+
| `ObjectDef` | Singleton objects | Scala 2 & 3 |
|
|
26
|
+
| `OpaqueType` | Opaque type aliases | Scala 3 (Scala 2 fallback to `type` alias) |
|
|
27
|
+
| `Newtype` | Zero-cost wrappers | Scala 2 & 3 |
|
|
28
|
+
| `TypeAlias` | Type aliases (with optional type parameters) | Scala 2 & 3 |
|
|
29
|
+
|
|
30
|
+
## Common Interface
|
|
31
|
+
|
|
32
|
+
Every `TypeDefinition` provides:
|
|
33
|
+
|
|
34
|
+
```scala
|
|
35
|
+
sealed trait TypeDefinition {
|
|
36
|
+
def name: String // The name of the type
|
|
37
|
+
def annotations: List[Annotation] // Annotations applied to it
|
|
38
|
+
def doc: Option[String] // Optional documentation
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Choosing the Right Variant
|
|
43
|
+
|
|
44
|
+
Choose the right `TypeDefinition` for your use case:
|
|
45
|
+
|
|
46
|
+
### Data Structures
|
|
47
|
+
|
|
48
|
+
**Case Class** — Immutable record with named fields and automatic `equals`, `hashCode`, `copy`:
|
|
49
|
+
|
|
50
|
+
```scala
|
|
51
|
+
import zio.blocks.codegen.ir._
|
|
52
|
+
|
|
53
|
+
val user = CaseClass(
|
|
54
|
+
name = "User",
|
|
55
|
+
fields = List(
|
|
56
|
+
Field("id", TypeRef.Long),
|
|
57
|
+
Field("name", TypeRef.String)
|
|
58
|
+
)
|
|
59
|
+
)
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
**Newtype** — Zero-cost wrapper for a single underlying type (e.g., `UserId` wrapping `Long`):
|
|
63
|
+
|
|
64
|
+
```scala
|
|
65
|
+
import zio.blocks.codegen.ir._
|
|
66
|
+
|
|
67
|
+
val userId = Newtype("UserId", TypeRef.Long)
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
**Type Alias** — Synonym for an existing type:
|
|
71
|
+
|
|
72
|
+
```scala
|
|
73
|
+
import zio.blocks.codegen.ir._
|
|
74
|
+
|
|
75
|
+
val stringId = TypeAlias("StringId", typeRef = TypeRef.String)
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
**Type Alias with Type Parameters** — Generic type alias:
|
|
79
|
+
|
|
80
|
+
```scala
|
|
81
|
+
import zio.blocks.codegen.ir._
|
|
82
|
+
|
|
83
|
+
val pair = TypeAlias(
|
|
84
|
+
"Pair",
|
|
85
|
+
typeRef = TypeRef("Tuple2", List(TypeRef("A"), TypeRef("B"))),
|
|
86
|
+
typeParams = List(TypeParam("A"), TypeParam("B"))
|
|
87
|
+
)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Sum Types (ADTs)
|
|
91
|
+
|
|
92
|
+
**Sealed Trait** — Discriminated union type with cases:
|
|
93
|
+
|
|
94
|
+
```scala
|
|
95
|
+
import zio.blocks.codegen.ir._
|
|
96
|
+
|
|
97
|
+
val payment = SealedTrait(
|
|
98
|
+
name = "Payment",
|
|
99
|
+
cases = List(
|
|
100
|
+
SealedTraitCase.CaseClassCase(
|
|
101
|
+
CaseClass("Card", List(Field("number", TypeRef.String)))
|
|
102
|
+
),
|
|
103
|
+
SealedTraitCase.CaseObjectCase("Cash")
|
|
104
|
+
)
|
|
105
|
+
)
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
**Enum** (Scala 3 only) — Enumeration with simple or parameterized cases:
|
|
109
|
+
|
|
110
|
+
```scala
|
|
111
|
+
import zio.blocks.codegen.ir._
|
|
112
|
+
|
|
113
|
+
val color = Enum(
|
|
114
|
+
name = "Color",
|
|
115
|
+
cases = List(
|
|
116
|
+
EnumCase.SimpleCase("Red"),
|
|
117
|
+
EnumCase.SimpleCase("Green"),
|
|
118
|
+
EnumCase.ParameterizedCase("Custom", List(Field("rgb", TypeRef.Int)))
|
|
119
|
+
)
|
|
120
|
+
)
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### Abstractions
|
|
124
|
+
|
|
125
|
+
**Trait** — Open trait for behavior and contracts:
|
|
126
|
+
|
|
127
|
+
```scala
|
|
128
|
+
import zio.blocks.codegen.ir._
|
|
129
|
+
|
|
130
|
+
val comparable = Trait(
|
|
131
|
+
name = "Comparable",
|
|
132
|
+
members = List(
|
|
133
|
+
ObjectMember.DefMember(
|
|
134
|
+
Method("compare", params = Nil, returnType = TypeRef.Int)
|
|
135
|
+
)
|
|
136
|
+
)
|
|
137
|
+
)
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
**Abstract Class** — Mix of fields and methods, but not instantiable:
|
|
141
|
+
|
|
142
|
+
```scala
|
|
143
|
+
import zio.blocks.codegen.ir._
|
|
144
|
+
|
|
145
|
+
val entity = AbstractClass(
|
|
146
|
+
name = "Entity",
|
|
147
|
+
fields = List(Field("id", TypeRef.Long))
|
|
148
|
+
)
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
**Object** — Singleton object with static-like members:
|
|
152
|
+
|
|
153
|
+
```scala
|
|
154
|
+
import zio.blocks.codegen.ir._
|
|
155
|
+
|
|
156
|
+
val utils = ObjectDef(
|
|
157
|
+
name = "Utils",
|
|
158
|
+
members = List(
|
|
159
|
+
ObjectMember.ValMember("Version", TypeRef.String, "\"1.0.0\"")
|
|
160
|
+
)
|
|
161
|
+
)
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### Type Aliases (Advanced)
|
|
165
|
+
|
|
166
|
+
**Opaque Type** (Scala 3) — Type-safe wrapper with an underlying type (distinct from `Newtype`):
|
|
167
|
+
|
|
168
|
+
```scala
|
|
169
|
+
import zio.blocks.codegen.ir._
|
|
170
|
+
|
|
171
|
+
val userId2 = OpaqueType("UserId", underlyingType = TypeRef.Long)
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## Common Operations
|
|
175
|
+
|
|
176
|
+
All `TypeDefinition` variants support these core operations:
|
|
177
|
+
|
|
178
|
+
### Accessing Components
|
|
179
|
+
|
|
180
|
+
```scala
|
|
181
|
+
import zio.blocks.codegen.ir._
|
|
182
|
+
|
|
183
|
+
val cc = CaseClass("User", List(Field("id", TypeRef.Long)))
|
|
184
|
+
|
|
185
|
+
cc.name // "User"
|
|
186
|
+
cc.annotations // List[Annotation] (empty by default)
|
|
187
|
+
cc.doc // Option[String] (documentation)
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### With Annotations
|
|
191
|
+
|
|
192
|
+
Add annotations (like `@deprecated` or custom annotations):
|
|
193
|
+
|
|
194
|
+
```scala
|
|
195
|
+
import zio.blocks.codegen.ir._
|
|
196
|
+
|
|
197
|
+
val annotated = CaseClass(
|
|
198
|
+
name = "OldAPI",
|
|
199
|
+
fields = Nil,
|
|
200
|
+
annotations = List(
|
|
201
|
+
Annotation("deprecated")
|
|
202
|
+
)
|
|
203
|
+
)
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### With Documentation
|
|
207
|
+
|
|
208
|
+
Include scaladoc or javadoc comments:
|
|
209
|
+
|
|
210
|
+
```scala
|
|
211
|
+
import zio.blocks.codegen.ir._
|
|
212
|
+
|
|
213
|
+
val documented = CaseClass(
|
|
214
|
+
name = "User",
|
|
215
|
+
fields = List(Field("id", TypeRef.Long)),
|
|
216
|
+
doc = Some("Represents a user in the system")
|
|
217
|
+
)
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
### With Derives
|
|
221
|
+
|
|
222
|
+
Add derives clauses for automatic typeclass derivation:
|
|
223
|
+
|
|
224
|
+
```scala
|
|
225
|
+
import zio.blocks.codegen.ir._
|
|
226
|
+
|
|
227
|
+
val derived = CaseClass(
|
|
228
|
+
name = "Point",
|
|
229
|
+
fields = List(
|
|
230
|
+
Field("x", TypeRef.Int),
|
|
231
|
+
Field("y", TypeRef.Int)
|
|
232
|
+
),
|
|
233
|
+
derives = List("Show", "Eq")
|
|
234
|
+
)
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
## Examples
|
|
238
|
+
|
|
239
|
+
Practical examples demonstrate common usage:
|
|
240
|
+
|
|
241
|
+
### Example 1: Combining Types in a File
|
|
242
|
+
|
|
243
|
+
A file with multiple `TypeDefinition` variants:
|
|
244
|
+
|
|
245
|
+
```scala
|
|
246
|
+
import zio.blocks.codegen.ir._
|
|
247
|
+
import zio.blocks.codegen.emit._
|
|
248
|
+
|
|
249
|
+
val file = ScalaFile(
|
|
250
|
+
packageDecl = PackageDecl("com.example"),
|
|
251
|
+
types = List(
|
|
252
|
+
// Case class
|
|
253
|
+
CaseClass("Person", List(Field("name", TypeRef.String))),
|
|
254
|
+
|
|
255
|
+
// Sealed trait (sum type)
|
|
256
|
+
SealedTrait(
|
|
257
|
+
"Result",
|
|
258
|
+
typeParams = List(TypeParam("T")),
|
|
259
|
+
cases = List(
|
|
260
|
+
SealedTraitCase.CaseClassCase(
|
|
261
|
+
CaseClass("Success", List(Field("value", TypeRef("T"))), typeParams = List(TypeParam("T")))
|
|
262
|
+
),
|
|
263
|
+
SealedTraitCase.CaseObjectCase("Failure")
|
|
264
|
+
)
|
|
265
|
+
),
|
|
266
|
+
|
|
267
|
+
// Object with static members
|
|
268
|
+
ObjectDef(
|
|
269
|
+
"Config",
|
|
270
|
+
members = List(
|
|
271
|
+
ObjectMember.ValMember("MaxRetries", TypeRef.Int, "3")
|
|
272
|
+
)
|
|
273
|
+
)
|
|
274
|
+
)
|
|
275
|
+
)
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
Emits:
|
|
279
|
+
|
|
280
|
+
```scala
|
|
281
|
+
import zio.blocks.codegen.emit._
|
|
282
|
+
ScalaEmitter.emit(file, EmitterConfig())
|
|
283
|
+
// res15: String = """package com.example
|
|
284
|
+
//
|
|
285
|
+
// case class Person(
|
|
286
|
+
// name: String,
|
|
287
|
+
// )
|
|
288
|
+
//
|
|
289
|
+
// sealed trait Result[T]
|
|
290
|
+
//
|
|
291
|
+
// object Result {
|
|
292
|
+
// case class Success[T](
|
|
293
|
+
// value: T,
|
|
294
|
+
// ) extends Result[T]
|
|
295
|
+
// case object Failure extends Result[T]
|
|
296
|
+
// }
|
|
297
|
+
//
|
|
298
|
+
// object Config {
|
|
299
|
+
// val MaxRetries: Int = 3
|
|
300
|
+
// }
|
|
301
|
+
// """
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
### Example 2: Type Parameters and Generic Types
|
|
305
|
+
|
|
306
|
+
Defining a polymorphic case class:
|
|
307
|
+
|
|
308
|
+
```scala
|
|
309
|
+
import zio.blocks.codegen.ir._
|
|
310
|
+
import zio.blocks.codegen.emit._
|
|
311
|
+
|
|
312
|
+
val box = CaseClass(
|
|
313
|
+
name = "Box",
|
|
314
|
+
fields = List(
|
|
315
|
+
Field("value", TypeRef("A"))
|
|
316
|
+
),
|
|
317
|
+
typeParams = List(
|
|
318
|
+
TypeParam("A")
|
|
319
|
+
)
|
|
320
|
+
)
|
|
321
|
+
|
|
322
|
+
val file = ScalaFile(
|
|
323
|
+
packageDecl = PackageDecl("com.example"),
|
|
324
|
+
types = List(box)
|
|
325
|
+
)
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
Emits:
|
|
329
|
+
|
|
330
|
+
```scala
|
|
331
|
+
import zio.blocks.codegen.emit._
|
|
332
|
+
ScalaEmitter.emit(file, EmitterConfig())
|
|
333
|
+
// res17: String = """package com.example
|
|
334
|
+
//
|
|
335
|
+
// case class Box[A](
|
|
336
|
+
// value: A,
|
|
337
|
+
// )
|
|
338
|
+
// """
|
|
339
|
+
```
|
|
340
|
+
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: type-ref
|
|
3
|
+
title: "TypeRef"
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
`TypeRef` represents a reference to a Scala type in the IR. It captures both simple types (like `String`, `Int`) and generic types (like `List[String]`, `Map[String, Int]`).
|
|
7
|
+
|
|
8
|
+
## Use Cases
|
|
9
|
+
|
|
10
|
+
- Specifying the types of fields in case classes
|
|
11
|
+
- Defining return types of methods
|
|
12
|
+
- Representing type arguments in generic types
|
|
13
|
+
- Building complex nested type expressions
|
|
14
|
+
|
|
15
|
+
## Construction
|
|
16
|
+
|
|
17
|
+
Create a simple type reference:
|
|
18
|
+
|
|
19
|
+
```scala
|
|
20
|
+
import zio.blocks.codegen.ir._
|
|
21
|
+
|
|
22
|
+
val stringType = TypeRef("String")
|
|
23
|
+
val intType = TypeRef("Int")
|
|
24
|
+
val booleanType = TypeRef("Boolean")
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
With type arguments for generics:
|
|
28
|
+
|
|
29
|
+
```scala
|
|
30
|
+
import zio.blocks.codegen.ir._
|
|
31
|
+
|
|
32
|
+
val listOfString = TypeRef("List", List(TypeRef("String")))
|
|
33
|
+
val mapStringInt = TypeRef("Map", List(TypeRef("String"), TypeRef("Int")))
|
|
34
|
+
val optionalLong = TypeRef("Option", List(TypeRef("Long")))
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Using factory methods from the companion object:
|
|
38
|
+
|
|
39
|
+
```scala
|
|
40
|
+
import zio.blocks.codegen.ir._
|
|
41
|
+
|
|
42
|
+
val string = TypeRef.String
|
|
43
|
+
val int = TypeRef.Int
|
|
44
|
+
val optional = TypeRef.optional(TypeRef.String)
|
|
45
|
+
val list = TypeRef.list(TypeRef.Int)
|
|
46
|
+
val map = TypeRef.map(TypeRef.String, TypeRef.Int)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Key Operations
|
|
50
|
+
|
|
51
|
+
All core operations are shown below:
|
|
52
|
+
|
|
53
|
+
### Accessing Components
|
|
54
|
+
|
|
55
|
+
Extract parts of a type reference:
|
|
56
|
+
|
|
57
|
+
```scala
|
|
58
|
+
import zio.blocks.codegen.ir._
|
|
59
|
+
|
|
60
|
+
val optional = TypeRef("Option", List(TypeRef.String))
|
|
61
|
+
|
|
62
|
+
optional.name // "Option"
|
|
63
|
+
optional.typeArgs // List[TypeRef] = List(TypeRef("String"))
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Building Nested Types
|
|
67
|
+
|
|
68
|
+
Compose type references:
|
|
69
|
+
|
|
70
|
+
```scala
|
|
71
|
+
import zio.blocks.codegen.ir._
|
|
72
|
+
val nestedList = TypeRef("List", List(
|
|
73
|
+
TypeRef("Option", List(TypeRef("String")))
|
|
74
|
+
))
|
|
75
|
+
// Represents: List[Option[String]]
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Primitive Types
|
|
79
|
+
|
|
80
|
+
`TypeRef` provides factory methods for built-in types:
|
|
81
|
+
|
|
82
|
+
```scala
|
|
83
|
+
import zio.blocks.codegen.ir._
|
|
84
|
+
|
|
85
|
+
TypeRef.Unit // Unit
|
|
86
|
+
TypeRef.Boolean // Boolean
|
|
87
|
+
TypeRef.Byte // Byte
|
|
88
|
+
TypeRef.Short // Short
|
|
89
|
+
TypeRef.Int // Int
|
|
90
|
+
TypeRef.Long // Long
|
|
91
|
+
TypeRef.Float // Float
|
|
92
|
+
TypeRef.Double // Double
|
|
93
|
+
TypeRef.String // String
|
|
94
|
+
TypeRef.BigInt // BigInt
|
|
95
|
+
TypeRef.BigDecimal // BigDecimal
|
|
96
|
+
TypeRef.Nothing // Nothing
|
|
97
|
+
TypeRef.Any // Any
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Generic Type Factories
|
|
101
|
+
|
|
102
|
+
Convenience methods simplify common generic patterns:
|
|
103
|
+
|
|
104
|
+
```scala
|
|
105
|
+
import zio.blocks.codegen.ir._
|
|
106
|
+
|
|
107
|
+
TypeRef.optional(TypeRef.String) // Option[String]
|
|
108
|
+
TypeRef.list(TypeRef.Int) // List[Int]
|
|
109
|
+
TypeRef.set(TypeRef.String) // Set[String]
|
|
110
|
+
TypeRef.map(TypeRef.String, TypeRef.Int) // Map[String, Int]
|
|
111
|
+
TypeRef.tuple(TypeRef.String, TypeRef.Int) // Tuple2[String, Int]
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Examples
|
|
115
|
+
|
|
116
|
+
Practical examples demonstrate common usage:
|
|
117
|
+
|
|
118
|
+
### Example 1: Field Types
|
|
119
|
+
|
|
120
|
+
Use `TypeRef` to define field types in a case class:
|
|
121
|
+
|
|
122
|
+
```scala
|
|
123
|
+
import zio.blocks.codegen.ir._
|
|
124
|
+
|
|
125
|
+
val user = CaseClass(
|
|
126
|
+
name = "User",
|
|
127
|
+
fields = List(
|
|
128
|
+
Field("id", TypeRef.Long),
|
|
129
|
+
Field("name", TypeRef.String),
|
|
130
|
+
Field("email", TypeRef.optional(TypeRef.String)),
|
|
131
|
+
Field("tags", TypeRef.list(TypeRef.String))
|
|
132
|
+
)
|
|
133
|
+
)
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### Example 2: Generic Type Parameters
|
|
137
|
+
|
|
138
|
+
Use `TypeRef` with type variable names:
|
|
139
|
+
|
|
140
|
+
```scala
|
|
141
|
+
import zio.blocks.codegen.ir._
|
|
142
|
+
|
|
143
|
+
val container = CaseClass(
|
|
144
|
+
name = "Container",
|
|
145
|
+
fields = List(
|
|
146
|
+
Field("value", TypeRef("T"))
|
|
147
|
+
),
|
|
148
|
+
typeParams = List(TypeParam("T"))
|
|
149
|
+
)
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Represents: `Container[T]` with a field `value: T`
|
|
153
|
+
|
|
154
|
+
### Example 3: Complex Nested Generics
|
|
155
|
+
|
|
156
|
+
Build deeply nested type expressions:
|
|
157
|
+
|
|
158
|
+
```scala
|
|
159
|
+
import zio.blocks.codegen.ir._
|
|
160
|
+
|
|
161
|
+
val complexType = TypeRef("Map", List(
|
|
162
|
+
TypeRef.String,
|
|
163
|
+
TypeRef("List", List(
|
|
164
|
+
TypeRef("Option", List(TypeRef.Int))
|
|
165
|
+
))
|
|
166
|
+
))
|
|
167
|
+
// Represents: Map[String, List[Option[Int]]]
|
|
168
|
+
|
|
169
|
+
val field = Field("data", complexType)
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### Example 4: Qualified Type Names
|
|
173
|
+
|
|
174
|
+
Use fully qualified names for non-standard types:
|
|
175
|
+
|
|
176
|
+
```scala
|
|
177
|
+
import zio.blocks.codegen.ir._
|
|
178
|
+
|
|
179
|
+
val bigDecimal = TypeRef("java.math.BigDecimal")
|
|
180
|
+
val jsonObject = TypeRef("com.example.json.JsonObject")
|
|
181
|
+
|
|
182
|
+
val field1 = Field("price", bigDecimal)
|
|
183
|
+
val field2 = Field("metadata", jsonObject)
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### Example 5: Union and Intersection Types (Scala 3)
|
|
187
|
+
|
|
188
|
+
Represent Scala 3 union and intersection types:
|
|
189
|
+
|
|
190
|
+
```scala
|
|
191
|
+
import zio.blocks.codegen.ir._
|
|
192
|
+
|
|
193
|
+
// Represents: String | Int
|
|
194
|
+
val unionType = TypeRef("|", List(TypeRef.String, TypeRef.Int))
|
|
195
|
+
|
|
196
|
+
// Represents: Serializable & Comparable
|
|
197
|
+
val intersectionType = TypeRef("&", List(
|
|
198
|
+
TypeRef("Serializable"),
|
|
199
|
+
TypeRef("Comparable")
|
|
200
|
+
))
|
|
201
|
+
```
|