Skip to content

Repository files navigation

@Buildable Swift Macro

@Buildable is an attached swift macro for structs, classes and enums, which produces a peer struct implementing the builder pattern. Initialise your object with minimal effort using default values.

import Buildable
@BuildablestructPerson{letname:Stringletage:Int}letperson=PersonBuilder(age:42).build()

Important!
- This macro is intended to be used for simple structs, enums and classes (see below limitations)
- Please report any issues you might encounter
- Please star this repository, if your project makes use of it :)

Table of Contents

Detailed Example with generated builder

import Buildable
@BuildablestructPerson{letname:Stringletage:Intletaddress:AddressletfavouriteSeason:Season}@BuildablepublicenumSeason{case winter
case spring
case summer
case autumn
}@Buildable(accessLevel:.internal)packageclassAppState{letpersons:[Person]init(
persons:[Person]){self.persons = persons
}}letanyPerson=PersonBuilder().build()letmax=PersonBuilder(name:"Max", favouriteSeason:.summer).build()letappState=AppStateBuilder(persons:[max]).build()

Expanded macro

structPersonBuilder{varname:String=""varage:Int=0varaddress:Address=AddressBuilder().build()varfavouriteSeason:Season=SeasonBuilder().build()func build()->Person{returnPerson(
name: name,
age: age,
address: address,
favouriteSeason: favouriteSeason
)}}publicstructSeasonBuilder{publicvarvalue:Season=.winter
publicinit(
value:Season=.winter
){self.value = value
}publicfunc build()->Season{return value
}}structAppStateBuilder{varpersons:[Person]=[]func build()->AppState{returnAppState(
persons: persons
)}}

Installation

The library can be installed using Swift Package Manager.

Features

  • The macro can be applied to struct, enum and class definitions to generate a builder
  • For struct definitions without explicit initialisers the macro makes a best guess to derive the memberwise initialiser. Please create a GitHub issue, in case you find any bugs :)
  • For every init-parameter a type dependent default value is set (see the below table). For unknown/custom types, the macro will expect another builder to be defined somewhere else
    • E.x. a known type: var number: Int = 0
    • E.x. an unknown type: var value: MyValue = MyValueBuilder().build()
  • By default the top level access level (e.x. public, private, etc.) of struct, enum and class definition is also appled to the builder. If you need the builder to have a lower access level you can define it via @Buildable(accessLevel: .internal)

Limitations

  • If a builder for a specific declaration can not be generated, you can always choose to create it yourself by following the below builder naming pattern:
    struct<MyType>Builder {func build()-><MyType>{return...}}
  • If a class or a struct has one or more initialisers, the macro will use the first/top one
  • As of Swift 6.2.0 and Xcode 26.0 (02.10.2025) it is not possible to use the generated builders inside the SwiftUI #Preview closure

Builder default values

The list of default values is limited to the values specified in the below table. If a type e.x. UnknownType is not part of the list, the macro will set the default value to UnknownTypeBuilder().build(), assuming that the UnknownTypeBuilder was created somewhere else.

TypeDefault Value
UnknownTypeUnknownTypeBuilder().build()
String""
Int0
Boolfalse
Double0
Float0
DateDate()
UUIDUUID()
[Any][]
[Any:Any][:]
Any?(implicitly nil)
Any!(implicitly nil)
() -> Void{}
(Any) -> Void{ _ in }
(Any, Any) -> Void{ _, _ in }
Int80
Int160
Int320
Int460
UInt0
UInt80
UInt160
UInt320
UInt460
DataData()
URLURL(string: "https://www.google.com")!
CGFloat0
CGPointCGPoint()
CGRectCGRect()
CGSizeCGSize()
CGVectorCGVector()

Roadmap

The @Buildable macro was created out of personal interest to reduce repetitive code in my own projects. I might continue developing the macro depending on use cases I stumble across, though, I do not guarantee to keep the project up to date myself. Please create GitHub issues for any feature or bugfix you would like to see within the macro. Contributions or fixes from the Community are most welcome.

About

An attached swift macro for structs, classes and enums, which produces a peer struct implementing the builder pattern. Initialise your object with minimal effort using default values.

Topics

Resources

Stars

22 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages