{"id":19031078,"url":"https://github.com/alexito4/unwraporthrow","last_synced_at":"2025-04-23T16:23:20.672Z","repository":{"id":63902026,"uuid":"444173598","full_name":"alexito4/UnwrapOrThrow","owner":"alexito4","description":"🎁 Unwrap an optional or throw an error if nil (or crash the program).","archived":false,"fork":false,"pushed_at":"2022-01-22T10:16:17.000Z","size":17,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-23T16:23:14.700Z","etag":null,"topics":["error-handling","error-reporting","optional","swift","unwrap"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":false,"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/alexito4.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}},"created_at":"2022-01-03T19:16:51.000Z","updated_at":"2022-11-30T10:57:47.000Z","dependencies_parsed_at":"2023-01-14T13:00:31.075Z","dependency_job_id":null,"html_url":"https://github.com/alexito4/UnwrapOrThrow","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexito4%2FUnwrapOrThrow","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexito4%2FUnwrapOrThrow/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexito4%2FUnwrapOrThrow/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexito4%2FUnwrapOrThrow/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alexito4","download_url":"https://codeload.github.com/alexito4/UnwrapOrThrow/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250468465,"owners_count":21435494,"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":["error-handling","error-reporting","optional","swift","unwrap"],"created_at":"2024-11-08T21:21:20.072Z","updated_at":"2025-04-23T16:23:20.613Z","avatar_url":"https://github.com/alexito4.png","language":"Swift","readme":"# Unwrap Or Throw (or Die!)\n\n[![Build and Test](https://github.com/alexito4/UnwrapOrThrow/actions/workflows/swift.yml/badge.svg)](https://github.com/alexito4/UnwrapOrThrow/actions/workflows/swift.yml)\n[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Falexito4%2FUnwrapOrThrow%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/alexito4/UnwrapOrThrow)\n[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Falexito4%2FUnwrapOrThrow%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/alexito4/UnwrapOrThrow)\n\n🎁 Unwrap an optional or throw an error if nil (or crash the program).\n\n\u003e Not invented here. The idea for **unwrap or die** and **unwrap or throw** has been around in the Swift community for years. You can read all about it in the [references](#References) below.\n\nRead more: [Unwrap Or Throw (or Die)](https://alejandromp.com/blog/unwrap-or-throw-or-die/)\n\n## Usage\n\nThis library provides different facilities to unwrap an `Optional` .\n\n\u003e Swift provides a way to go from a throwing function to an optional `try?` but it doesn't have an easy way to do the reverse operation.\n\n### Coalescing with `raise`\n\nSwift's `throw` is not an expression so we can't use it on the right side of the coalescing operator `??`. The `raise` function supplies this functionality. It’s a very simple function that throws the given error, but it makes it possible to use it in places where an expression is needed.\n\n```swift\ntry someWork() ?? raise(YourError())\n```\n\n### Coalescing with `??`\n\nAn overload of `??` is provided to remove the need for the `raise` function. If you prefer to be succinct instead of explicit, you can just use an error on the right of the operator and it will just work.\n\n```swift\ntry someWork() ?? YourError()\n```\n\n## Method `unwrapOrThrow`\n\nIf you don't like to abuse the coalescing operator to throw errors, you can instead use a method variant.\n\n```swift\ntry someWork().unwrapOrThrow(YourError())\n```\n\nThe library comes with a default error: `UnwrapError`. So you can call the method without specifying a custom error.\n\n```swift\ntry someWork().unwrapOrThrow()\n```\n\n## ☠️ Unwrap or die\n\nDespite the name of the library, you can also crash the program instead of throwing an error.\n\nYou can use the same techniques as when throwing errors but using `fatalError(\"reason for crashing\")` instead:\n\n```swift\ntry someWork() ?? raise(fatalError(\"reason for crashing\"))\n```\n\nThe compiler will show a warning on this line `Will never be executed` so instead of using `raise` you can directly `fatalError` on the right side of the `??` operator.\n\n```swift\ntry someWork() ?? fatalError(\"reason for crashing\")\n```\n\nOr if you prefer the method, you can also use it:\n\n```swift\ntry someWork().unwrapOrThrow(fatalError(\"reason for crashing\"))\n```\n\nThis has the same result as force unwrapping `!` but with a better error message to help you see the problems on the logs.\n\n\u003e A barely use the *unwrap or die* since when I want to force unwrap I just do that and I don't find the lack of proper message an issue most of the time.\n\u003e\n\u003e That's why I don't need to introduce a facility to just pass a reason `String` directly into the unwrap operation. \n\n## References\n\n- [johnno1962/Unwrap](https://github.com/johnno1962/Unwrap)\n- [Introducing an “Unwrap or Throw” operator](https://forums.swift.org/t/introducing-an-unwrap-or-throw-operator/51905) \n- [Unwrap or Throw - Make the Safe Choice Easier](https://forums.swift.org/t/unwrap-or-throw-make-the-safe-choice-easier/14453) \n- [[Pitch] Introducing the “Unwrap or Die” operator to the standard library](https://forums.swift.org/t/pitch-introducing-the-unwrap-or-die-operator-to-the-standard-library/6207) \n- [SE-0217 - The “Unwrap or Die” operator](https://forums.swift.org/t/se-0217-the-unwrap-or-die-operator/14107) \n\n# Author\n\nAlejandro Martinez | https://alejandromp.com | [@alexito4](https://twitter.com/alexito4)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexito4%2Funwraporthrow","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexito4%2Funwraporthrow","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexito4%2Funwraporthrow/lists"}