Field Configuration
This page covers macro-level field configuration in Swift.
Available Macro Attributes
@ForyObjecton struct/class/enum@ForyField(encoding: ...)on numeric fields
@ForyField(encoding:)
Use @ForyField to override integer encoding strategy.
@ForyObject
struct Metrics: Equatable {
@ForyField(encoding: .fixed)
var u32Fixed: UInt32 = 0
@ForyField(encoding: .tagged)
var u64Tagged: UInt64 = 0
}
Supported combinations
| Swift type | Supported encoding values | Default encoding |
|---|---|---|
Int32 | .varint, .fixed | .varint |
UInt32 | .varint, .fixed | .varint |
Int64 | .varint, .fixed, .tagged | .varint |
UInt64 | .varint, .fixed, .tagged | .varint |
Int | .varint, .fixed, .tagged | .varint |
UInt | .varint, .fixed, .tagged | .varint |
Compile-time validation rejects unsupported combinations (for example, Int32 with .tagged).
@ForyObject Requirements
Struct and class fields
- Stored properties must declare explicit types
- Computed properties are ignored
- Static/class properties are ignored
Class requirement
Classes annotated with @ForyObject must provide a required init() for default construction.
@ForyObject
final class Node {
var value: Int32 = 0
var next: Node? = nil
required init() {}
}
Dynamic Any Fields in Macro Types
@ForyObject supports dynamic fields and nested containers:
Any,AnyObject,any SerializerAnyHashable[Any][String: Any][Int32: Any][AnyHashable: Any]
Current limitations:
Dictionary<K, Any>is only supported whenKisString,Int32, orAnyHashable