Coder Social home page Coder Social logo

kotlin-when-matches's Introduction

Better when statements

Regular when statements

When statements in Kotlin can be defined in two ways. Both have disadvantages.

With subject

val string = "Hello World!"
val i = when (string) {
    "Hello" -> 1
    "World" -> 2
    in listOf("foo") -> 3
    else -> 4
}

This syntax only allows equality, contains and instance checks. However it is not possible to check anything different like checking if the length of the string is even. For that we have to use when without argument.

Without subject

val string = "Hello World!"
val i = when {
    string == "Hello" -> 1
    string.length % 2 == 0 -> 2
    string in listOf("foo") -> 3
    else -> 4
}

This syntax gives you more freedom. You can check anything you want. But it's a bit verbose, since you have to use the variable string in every line.

when statements with the extension

The extension makes when statements work as follows:

val string = "Hello World!"
val i = when (string.matches) {
    "Hello" -> 1
    String::isNotEmpty -> 2
    { it: String -> it.length % 2 == 0 } -> 3
    else -> 4
}

When you call the getter of the property matches on an object, you can also use method references to check a predicate. As consequence you can also use lambdas to check if a predicate is fullfilled.

You may ask why this works. This feature is an abuse of the equals function. The function matches returns an object, which overrides the equals method such that it returns true, if this value equals the other value or if the other value is a predicate (a function taking the value's type as argument and returns a Boolean) which returns true for this value.

For the exact implementation have a look at the file Match.kt.

kotlin-when-matches's People

Contributors

leongeorgi avatar

Stargazers

Leah Amelia Chen avatar

Watchers

James Cloos 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.