Skip to content

onuniverse/swift-experimental-reflection

 
 

Repository files navigation

KeyPath Reflection

Note: This package is a part of a Swift Evolution proposal for inclusion in the Swift standard library, and is not intended for use in production code at this time.

Introduction

This proposal aims to provide a mechanism for users to get key paths to stored properties of types at runtime.

struct Dog {
  let age: Int
  let name: String
}

let dogKeyPaths = Reflection.allKeysPaths(for: Dog.self)

let sparky = Dog(age: 128, name: "Sparky")

for dogKeyPath in dogKeyPaths {
  print(sparky[keyPath: dogKeyPath]) // 128, Sparky
}

Of course, this also works with instances at runtime:

let nums = [1, 2, 3, 4]
let numKeyPaths = Reflection.allKeyPaths(for: nums)

for numKeyPath in numKeyPaths {
  print(nums[keyPath: numKeyPath]) // 1, 2, 3, 4
}

Usage

To use this library in a Swift Package Manager project, add the following to your Package.swift file's dependencies:

.package(
    url: "https://github.com/apple/swift-evolution-staging.git",
    .branch("SE0000_KeyPathReflection")
)

About

No description, website, or topics provided.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 96.4%
  • C 3.6%