跳到主要内容
版本:dev

Field Configuration

This page covers macro-level field configuration in Swift.

Available Macro Attributes

  • @ForyObject on 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 typeSupported encoding valuesDefault 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 Serializer
  • AnyHashable
  • [Any]
  • [String: Any]
  • [Int32: Any]
  • [AnyHashable: Any]

Current limitations:

  • Dictionary<K, Any> is only supported when K is String, Int32, or AnyHashable