Code Monkey home page Code Monkey logo

swift-conformable-existential's Introduction

SwiftConformableExistential

A set of Swift Macros designed to facilitate the conformance of existential types to Equatable, Hashable, Decodable, Encodable, and Codable.

Overview

Until Swift gains support for extending existential types, they cannot satisfy generic constraints on their own.

protocol Drinkable: Hashable {}

// Is ill-formed, because `any Drinkable` does not conform to `Hashable`,
// despite the fact that every `Drinkable` instance does.
struct Foo: Hashable {
    let drinkable: any Drinkable
}

Overcoming this limitation requires extensive boilerplate code. This package offers a set of macros that can be attached to your protocol to synthesize ready-to-use property wrappers over the existential type of the annotated protocol. These wrappers act as proxies for the respective conformances.

import SwiftConformableExistential

@HashableExistential
protocol Drinkable: Hashable {}

// `HashableDrinkable` is a `Hashable` wrapper over `any Drinkable`,
// synthesized of the attached `@HashableExistential` macro,
// allowing the compiler to now synthesize the `Hashable` implementation for `Foo`.
struct Foo: Hashable {

    @HashableDrinkable
    var drinkable: any Drinkable
}

Alternatively, the wrappers can also be used on-the-fly, particularly in situations where employing property wrappers is not suitable:

struct Tap: View {

    @State private var drinkable: any Drinkable = .smallBeer
    
    var body: some View {
        ...           
        .onChange(of: HashableDrinkable(drinkable)) { _, newValue in
            prepareForPouring(newValue.wrappedValue)
        }
    }
}

For more, see the documentation.

Installation

Via SwiftPM.

In your package dependencies:

.package(url: "https://github.com/stansmida/swift-conformable-existential.git", from: "0.4.0"),

And in your target dependencies:

.product(name: "SwiftConformableExistential", package: "swift-conformable-existential"),

And don't forget to import in files where annotating your protocols:

import SwiftConformableExistential

swift-conformable-existential's People

Contributors

stansmida avatar

Stargazers

 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.