Giter VIP home page Giter VIP logo

swiftwhen's Introduction

SwiftWhen

What is SwiftWhen?

SwiftWhen is a syntax sugar that imitates the When Expression in Kotlin, as an addition to control flow.

Example

let isFreeUser: Bool = ...
let hasEnoughPoints: Bool = ...
let label = when {
    isFreeUser => "Please upgrade to premium user"
    hasEnoughPoints => "Click to redeam"
} ?? "Not enough points"

Installation

SwiftPackageManager

dependencies: [
    .package(url: "https://github.com/lionhylra/SwiftWhen.git", .upToNextMajor(from: "1.0.0"))
]

Documentation

When expression allows you to evaluate multiple confitions(boolean values) and map them to a target value. A custom operator => is used to construct a single mapping.

Boolean Expression => Value

You can put multiple expressions in the when function. The evaluation goes from top to bottom. The first map whose condition is true get selected and its value is returned as the return value of when function. If a mapping is selected, other mappings after that won't be evaluated.

let lastName: String? = ...
let gender: Gender = ...
let greetings = when {
    lastName == nil => "Hi there"
    lastName!.count > 5 => Hi \(lastName!)
    gender == .male => "Hi Mr \(lastName!)"
    gender == .female => "Hi Ms \(lastName!)"
}!

when expression always return an optional value. If no condition is met, when expression returns nil.

let num = -1
let text = when {
    num > 0 => "positive"
    num == 0 => "zero"
}
// text == nil

In a mapping, you can use bool value, bool expression or a function that returns bool as condition. You can also use a closure as a result value.

let n: Int = ...
let text = when {
    n > 0 => "a"
    n == 0 => { "b" }
    n < 0 => { () -> String in 
        let a = "a"
        let b = "b"
        return a + " " + b
    }
}

When to use it

when expression should not be used to replace if statement or switch statement. It can help improve the readability when it requires more lines to write using if of switch. It is convenient you have more than two conditions where swift ternary conditional operator is not enough, especially when you just want to return a value instead of doing a lot.

swiftwhen's People

Contributors

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