Giter Site home page Giter Site logo

enhancedmirror's Introduction

EnhancedMirror

An experimental Mirror alternative that utilizes Swift Macros for static reflection.

Features

  • Manipulations are done by compile-time generated code, no additional metadata hacks are required.
  • Ordered enumeration supports for field members.
  • Fields of both struct and class can be modified.

Quick Start

Note: Swift 5.9 is in preview, and not yet stable.

To use EnhancedMirror in your project, add this repository to the Package.swift manifest:

// swift-tools-version:5.9
import PackageDescription

let package = Package(
  name: "MyPackage",
  dependencies: [
    .package(url: "https://github.com/unixzii/EnhancedMirror.git", from: "0.1.0"),
  ],
  targets: [
    .target(name: "MyApp", dependencies: [
      .product(name: "EnhancedMirror", package: "EnhancedMirror"),
    ]),
  ]
)

Add annotation

To make a type inspectable at run-time, you can place @RuntimeInspectable macro at your type declaration:

@RuntimeInspectable
struct Product {
    let modelName: String
    var price: Int

    // ...
}

Use reflection APIs

An inspectable type conforms to RuntimeInspectable protocol, and you can use the APIs that protocol exposes:

let product = Product(...)

let priceField = product.field(named: "price")!

// Read the field value:
print(priceField.value)

// Write the field value:
priceField.write(999)

See RuntimeInspectable for the full APIs.

Notices

Lifetime of the field accessor

The field accessor doesn't retain a copy of value-type values. You must not make it outlive the inspected value, or the memory corruption may happen. For reference-type values, there are no such restrictions because the inspected values are strongly retained.

Project completeness

The project is not complete in current stage, but the important building blocks (accessing the value) are done. Some high-level APIs need to be designed for more ergonomic developer experience, such as recursive (de)serialization, field annotation, etc. Those features may be deferred until the stable version of Swift 5.9 releases.

While the project is at the experimental stage, you can still try it for any non-production usage.

License

Licensed under MIT License, see LICENSE for more information.

enhancedmirror's People

Contributors

flisky avatar unixzii avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.