Giter Site home page Giter Site logo

kiam's Introduction

kiam

CI status crates.io documentation (docs.rs) documentation (master) LICENSE

("kiam" is "when" in Esperanto)

This crate introduces when! macro which provides better syntax for if/else if/else chains. The syntax is similar to match.

(idea is borrowed from kotlin)

[dependencies] 
kiam = "0.1"

Usage

Usage is similar to the usage of match, but instead of patterns, branches are guarded by boolean expression:

kiam::when! {
    false => (),
    true => (),
    // ...
}

_ can be used as a default branch (it's also required to use when! in expression possition):

let x = kiam::when! {
    false => 0,
    _ => 1,
};

assert_eq!(x, 1);

You can also use let <pat> = to match a pattern, but in difference with match you'll have to provide an expression for every pattern:

let a = None;
let b = Some(17);
let fallible = || Err(());

let x = kiam::when! {
    let Some(x) = a => x,
    let Ok(x) = fallible() => x,
    let Some(x) = b => (x as u32) + 1,
    _ => 1,
};

assert_eq!(x, 18);

Last notes:

  • You can also compare structure litetals without brackets (you can't do this with if/else if/else chain)
  • You can mixup boolean-braches with pattern matching
  • Only one branch is executed (not to be confused with switch in C-like languages)
let mut x = 0;

kiam::when! {
    let Ok(_) = Err::<(), _>(()) => x = 1,
    true => x = 2,
    true => x = 3,
    let Some(n) = Some(42) => x = n,
};

assert_eq!(x, 2);
#[derive(PartialEq)]
struct Struct { a: i32 }

// This does not compile because of the ambiguity
if Struct { a: 0 } == Struct { a: 0 } {
    // ...
}
#[derive(PartialEq)]
struct Struct { a: i32 }

kiam::when! {
    // This, on the other hand, compiles fine
    Struct { a: 0 } == Struct { a: 0 } => {
        // ...
    },
}

kiam's People

Contributors

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