StaticMemberIterable

0.1.1

Confidently iterate through static members with this Swift Macro
DanielSincere/StaticMemberIterable

What's New

Add diagnostic warning when no static members exist.

2023-06-09T11:09:20Z

Add diagnostic warning when no static members exist. This would generate an untyped list: 'static let allStaticMembers = []' which does not compile

StaticMemberIterable

Confidently cover all static members. Like CaseIterable, this macro creates an array of all the static members of a type. This is useful when a type has a few examples as static members.

Here, we have a Chili type so that we can discuss the heat level and names of various chilis. So far, we have two: jalapeño and habenero. For testing or for displaying in the UI, we want to confidently list both the jalapeño & the habenero. With @StaticMemberIterable, we can finally do si.

@StaticMemberIterable
struct Chili {
  let name: String
  let heatLevel: Int
  
  static let jalapeño = Chili(name: "jalapeño", heatLevel: 2)
  static let habenero = Chili(name: "habenero", heatLevel: 5)
}

expands to

struct Chili {
  let name: String
  let heatLevel: Int
  
  static let jalapeño = Chili(name: "jalapeño", heatLevel: 2)
  static let habenero = Chili(name: "habenero", heatLevel: 5)
  
  static let allStaticMembers = [jalapeño, habenero]
}

Installation

In Package.swift, add the package to your dependencies.

.package(url: "https://github.com/FullQueueDeveloper/StaticMemberIterable.git", from: "1.0.0"),

And add "StaticMemberIterable" to the list of your target's dependencies.

When prompted by Xcode, trust the macro.

Swift macros?

Introduced at WWDC '23, requiring Swift 5.9

License

BSD-3-Clause

Description

  • Swift Tools 5.9.0
View More Packages from this Author

Dependencies

Last updated: Mon Apr 29 2024 18:01:18 GMT-0900 (Hawaii-Aleutian Daylight Time)