{"id":31009436,"url":"https://github.com/idrougge/kotlinguard","last_synced_at":"2025-09-13T04:37:17.539Z","repository":{"id":198635872,"uuid":"137351837","full_name":"idrougge/KotlinGuard","owner":"idrougge","description":"Kotlin implementation of Swift \"guard\" statement","archived":false,"fork":false,"pushed_at":"2018-06-14T11:55:31.000Z","size":1,"stargazers_count":13,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2023-10-06T07:39:24.612Z","etag":null,"topics":["kotlin-extensions","null-check"],"latest_commit_sha":null,"homepage":null,"language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/idrougge.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2018-06-14T11:54:16.000Z","updated_at":"2023-10-06T07:39:26.086Z","dependencies_parsed_at":null,"dependency_job_id":"6ccda898-be75-4910-b2dc-725c3a1f8f41","html_url":"https://github.com/idrougge/KotlinGuard","commit_stats":null,"previous_names":["idrougge/kotlinguard"],"tags_count":null,"template":null,"template_full_name":null,"purl":"pkg:github/idrougge/KotlinGuard","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/idrougge%2FKotlinGuard","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/idrougge%2FKotlinGuard/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/idrougge%2FKotlinGuard/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/idrougge%2FKotlinGuard/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/idrougge","download_url":"https://codeload.github.com/idrougge/KotlinGuard/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/idrougge%2FKotlinGuard/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":274919961,"owners_count":25373953,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-13T02:00:10.085Z","response_time":70,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["kotlin-extensions","null-check"],"created_at":"2025-09-13T04:37:12.603Z","updated_at":"2025-09-13T04:37:17.517Z","avatar_url":"https://github.com/idrougge.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Guard for Kotlin\n\n## What? ##\nThis generic function adds a mechanism similar to the `guard` statement in Swift to Kotlin. The `guard` mechanism checks a precondition (usually a `null` check), optionally binding variables if the precondition worked out. If a precondition fails, an `else` block is executed, which must end with a statement to leave the current scope, forcing an early return.\n\n## Usage ##\nIn Swift, this is expressed as: \n```swift\nguard let aa = a else { return }\n```\nHowever, this implementation follows the usual pattern of Kotlin standard helper function such as `apply` or `let`:\n```kotlin\nval w = v.guard{\n    println(\"Value was null!\")\n    return\n}\n```\n\n## Advanced usage ##\nUnlike Swift's `guard`, which can take any number and kind of preconditions, this leans on other standard Kotlin functions for additional conditions to be tested, e.g.:\n```kotlin\nval w = value\n        .takeIf { it \u003c 256 }\n        .guard {\n            println(\"value was either null or too big\")\n            return\n        }\n```\n\n## Rationale ##\nThe canonical Kotlin way to check for `null` and otherwise return is either using the \"Elvis\" operator `?:` or a construct such as `let{}` or `apply{}`. Using an \"Elvis return\", a function contains expressions such as: \n```kotlin\nval unwrapped = maybe ?: return // With optional return value\n```\nThe right-hand side of an Elvis expression can only be a single statement, even though you may want to log the error before the return, or forcing a retry.\n\nThe `let` or `takeIf` methods either require the entire logic for the \"safe\" value to be moved into the accompanying block, building upon the \"pyramid\" structure of code, or assignment followed by another Elvis for an early return.\n\nUsing a `guard`, any error handling takes place in the adjacent block, while the \"usual\" logic of the function goes on below the guard, at the usual indentation scope and indentation level. The guard also forces you to exit early, facilitating reasoning about the core logic of the function at hand, since each time you see a `guard`, you know that one source of errors has been excluded.\n\n## Example code ##\n```kotlin\nfun test() {\n    var maybe:String? = \"something\" // maybe is a nullable string\n    println(maybe is String) // true\n\n    val unwrapped = maybe.guard{\n    \tprintln(\"This would only run if `maybe` was null\")\n    \treturn\n    }\n    println(unwrapped is String) // true\n\n    maybe = null\n    println(maybe is String) // false\n    \n    val never = maybe.guard{\n    \tprintln(\"Found a null value, exiting function\")\n    \treturn\n    }\n    println(\"This line is unreachable because `maybe` is set to null\")\n}\n\nfun main(args: Array\u003cString\u003e) {\n\ttest()\n\tprintln(\"Back in main\")\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fidrougge%2Fkotlinguard","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fidrougge%2Fkotlinguard","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fidrougge%2Fkotlinguard/lists"}