{"id":15722322,"url":"https://github.com/stackotter/swift-bare-last-exprs","last_synced_at":"2025-03-30T23:43:39.696Z","repository":{"id":212627716,"uuid":"731944530","full_name":"stackotter/swift-bare-last-exprs","owner":"stackotter","description":"A Swift macro that brings Rust's bare last expression syntax to Swift for multi-statement expressions with implicit returns.","archived":false,"fork":false,"pushed_at":"2023-12-15T09:09:57.000Z","size":13,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-06T05:17:54.190Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stackotter.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-12-15T08:56:27.000Z","updated_at":"2023-12-15T08:56:40.000Z","dependencies_parsed_at":null,"dependency_job_id":"fc9ae5ae-6c71-4b20-823c-02a659be000b","html_url":"https://github.com/stackotter/swift-bare-last-exprs","commit_stats":{"total_commits":7,"total_committers":1,"mean_commits":7.0,"dds":0.0,"last_synced_commit":"4dc36ba8ca088982b9db2513330d345185711b35"},"previous_names":["stackotter/swift-bare-last-exprs"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stackotter%2Fswift-bare-last-exprs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stackotter%2Fswift-bare-last-exprs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stackotter%2Fswift-bare-last-exprs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stackotter%2Fswift-bare-last-exprs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stackotter","download_url":"https://codeload.github.com/stackotter/swift-bare-last-exprs/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246395572,"owners_count":20770240,"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","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":[],"created_at":"2024-10-03T22:06:41.387Z","updated_at":"2025-03-30T23:43:39.677Z","avatar_url":"https://github.com/stackotter.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Bare last expressions\n\nThis macro was built to try out a potential syntax for multi-statement if/switch expressions\nin Swift before [the pitch for multi-statement if/switch expressions](https://forums.swift.org/t/pitch-multi-statement-if-switch-do-expressions/68443/443)\nlands.\n\nWhen a function is annotated with `@BareLastExprs`, the last expression in a multi-statement\nif/switch expression branch is used as the result. For fullness, the last expression in\na function or closure body is implicitly returned. Together these allow for quite neat code in\nmy opinion.\n\nThe following code sample shows off a variety of complex expressions with implicit returns\npowered by the `@BareLastExprs` function body macro.\n\n```swift\n@BareLastExprs\nfunc fortune(_ number: Int) -\u003e String {\n    print(\"Requesting fortune for \\(number)\")\n    let fortune = switch number {\n        case 1, 3, 5:\n            print(\"Warning: Support for odd numbers is unstable\")\n            if number == 3 {\n                \"You have a long and prosperous future\"\n            } else {\n                \"Your future looks bleak\"\n            }\n        case 2, 4, 6:\n            if number == 6 {\n                \"You must watch your back tomorrow (good luck...)\"\n            } else {\n                \"Your shoes will develop an untimely hole\"\n            }\n        default:\n            print(\"Warning: I've never encountered \\(number) before\")\n            \"Spaghetti will fall, meatballs will rise\"\n    }\n\n    print(\"Processing...\")\n\n    if Int.random(in: 0..\u003c10) == 0 {\n        print(\"Warning: Quantum interference detected in RAM\")\n        \"Fortune got corrupted, please try again\"\n    } else {\n        fortune\n    }\n}\n```\n\n### Usage\n\nSince function body macros haven't landed yet, you'll have to download the latest toolchain\nsnapshot and enable the experimental `BodyMacros` features. I've been using\n[the 2023-12-07 toolchain (macOS)](https://download.swift.org/development/xcode/swift-DEVELOPMENT-SNAPSHOT-2023-12-07-a/swift-DEVELOPMENT-SNAPSHOT-2023-12-07-a-osx.pkg),\nbut any newer toolchains should also work. I haven't tested the macro on Linux yet, let me know\nif it works!\n\n```sh\n/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-XXXX-XX-XX-x.xctoolchain/usr/bin/swift run -Xswiftc -enable-experimental-feature -Xswiftc BodyMacros\n```\n\nBe aware that you may get weird compiler crashes if you try to run the example with **Xcode**,\nI'm not really sure what's causing it, but I don't think it's the macro's fault. Copying\nand pasting the expanded code and compiling that runs fine.\n\n### Known limitations\n\nThe macro wraps code blocks in closures to implement bare last expressions. Which means\nthat assignments to uninitialized variables won't compile (sorry definite initialization).\n\n```swift\nlet opposite: Int\nlet value = if condition {\n    opposite = 0 // cannot assign to value: 'opposite' is a 'let' constant\n    1\n} else {\n    opposite = 1 // cannot assign to value: 'opposite' is a 'let' constant\n    0\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstackotter%2Fswift-bare-last-exprs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstackotter%2Fswift-bare-last-exprs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstackotter%2Fswift-bare-last-exprs/lists"}