Giter Site home page Giter Site logo

yalishanda42 / options Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 0.0 234 KB

Easily create OptionSets from enums in Swift.

Home Page: http://yalishanda42.github.io/Options/

License: MIT License

Swift 100.00%
swift syntax-sugar optionset enum bitflags

options's Introduction

Options

Made for Swift

Swift Package Manager supported

Build and test GitHub

The swiftier OptionSet. With the help of enums.

The Problem

Let's say that you have some model in your code, for example named Card. This model has a property color which aims to represent its color. Let's also say that all possible card colors are white, blue, black, red, green and all possible combinations of those 5, including none of them.

The standard way this can be handled now in Swift is to create an OptionSet that has flags assigning all colors the numbers 1, 2, 4, 8, 16, ... (so that they can be easily combined with the bitwise operator |), like so:

struct CardColor: OptionSet {
    let rawValue: Int

    init(rawValue: Int) {
        self.rawValue = rawValue
    }

    static let white = CardColor(rawValue: 1 << 0)
    static let blue  = CardColor(rawValue: 1 << 1)
    static let black = CardColor(rawValue: 1 << 2)
    static let red   = CardColor(rawValue: 1 << 3)
    static let green = CardColor(rawValue: 1 << 4)
}

The code snippet above shows the bare minimum for creating an OptionSet that would suit our needs. Although rather simple and straightforward, it still contains quite some boilerplate for just 5 flags: a stored property, an initializer, as well as all literal raw values for all flags manually written.

Our "swifty" intuition tells us that we can do better, can't we?

The Solution

Create a regular enum with all options. They don't need to have a raw value, the only condition is that the enum should conform to CaseIterable (because the way they are arranged in the .allCases array will determine their raw values).

enum CardColor: CaseIterable {
    case white
    case blue
    case black
    case red
    case green
}

Now in order to use the enum like an OptionSet we just need to use the Options struct provided in this library:

let colorless: Options<CardColor> = []
let onlyWhite: Options<CardColor> = [.white]
let bluAndRed: Options<CardColor> = [.blue, .red]

No need for boilerplates and supplying the raw values of all flags.

Extras

The following convenience properties and methods exist:

let allCombined: Options<CardColor> = .all // Equivalent to Options<Topping>([.white, .blue, .black, .red, .green])

let allPossibleBlueColorCombinations = Options<CardColor>.allContaining(.blue) // returns a list of all elements which contain `[.blue]`, sorted ascendingly by their raw values

let favoriteColorCombo: Options<CardColor> = [.white, .blue, .red]
let allPossibleFavoriteColorCombinations = Options<CardColor>.allContaining(favoriteColorCombo) // returns a list of all elements which contain `[.white, .blue, .red]`, sorted ascendingly by their raw values
let sameAsAbove = favoriteColorCombo.allCombinationsContainingSelf

let favoriteComboArray = favoriteColorCombo.decomposed // this is a [CardColor] array containing [.white, .blue, .red]

Limitations

  1. The square brackets should be present even if creating an instance just by using one option, for example:
Options<CardColor>.all.contains(.white)    // this will not compile
Options<CardColor>.all.contains([.white]) //  this will solve it
  1. The supplied enum needs to have a maximum of 64 cases.

License

MIT

options's People

Contributors

yalishanda42 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  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.