{"id":1028,"url":"https://github.com/antitypical/Result","last_synced_at":"2025-08-06T16:32:07.523Z","repository":{"id":27701662,"uuid":"31188467","full_name":"antitypical/Result","owner":"antitypical","description":"Swift type modelling the success/failure of arbitrary operations.","archived":false,"fork":false,"pushed_at":"2021-04-22T18:55:51.000Z","size":397,"stargazers_count":2507,"open_issues_count":11,"forks_count":227,"subscribers_count":39,"default_branch":"master","last_synced_at":"2024-10-29T15:34:42.839Z","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/antitypical.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-02-23T00:41:05.000Z","updated_at":"2024-08-23T18:50:07.000Z","dependencies_parsed_at":"2022-06-26T06:38:26.795Z","dependency_job_id":null,"html_url":"https://github.com/antitypical/Result","commit_stats":null,"previous_names":[],"tags_count":41,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antitypical%2FResult","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antitypical%2FResult/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antitypical%2FResult/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antitypical%2FResult/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/antitypical","download_url":"https://codeload.github.com/antitypical/Result/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222177845,"owners_count":16943745,"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-01-05T20:15:37.376Z","updated_at":"2024-12-09T16:31:18.026Z","avatar_url":"https://github.com/antitypical.png","language":"Swift","funding_links":[],"categories":["Data Structures / Algorithms","Libs","Swift","Data Structure","Utilities and Extensions","Utility [🔝](#readme)","The Index"],"sub_categories":["Getting Started","Utility","Other free courses","Linter","Workflow-enabler Frameworks"],"readme":"# Result\n\n[![Build Status](https://travis-ci.org/antitypical/Result.svg?branch=master)](https://travis-ci.org/antitypical/Result)\n[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)\n[![CocoaPods](https://img.shields.io/cocoapods/v/Result.svg)](https://cocoapods.org/)\n[![Reference Status](https://www.versioneye.com/objective-c/result/reference_badge.svg?style=flat)](https://www.versioneye.com/objective-c/result/references)\n\nThis is a Swift µframework providing `Result\u003cValue, Error\u003e`.\n\n`Result\u003cValue, Error\u003e` values are either successful (wrapping `Value`) or failed (wrapping `Error`). This is similar to Swift’s native `Optional` type: `success` is like `some`, and `failure` is like `none` except with an associated `Error` value. The addition of an associated `Error` allows errors to be passed along for logging or displaying to the user.\n\nUsing this µframework instead of rolling your own `Result` type allows you to easily interface with other frameworks that also use `Result`.\n\n## Use\n\nUse `Result` whenever an operation has the possibility of failure. Consider the following example of a function that tries to extract a `String` for a given key from a JSON `Dictionary`.\n\n```swift\ntypealias JSONObject = [String: Any]\n\nenum JSONError: Error {\n    case noSuchKey(String)\n    case typeMismatch\n}\n\nfunc stringForKey(json: JSONObject, key: String) -\u003e Result\u003cString, JSONError\u003e {\n    guard let value = json[key] else {\n        return .failure(.noSuchKey(key))\n    }\n    \n    guard let value = value as? String else {\n        return .failure(.typeMismatch)\n    }\n\n    return .success(value)\n}\n```\n\nThis function provides a more robust wrapper around the default subscripting provided by `Dictionary`. Rather than return `Any?`, it returns a `Result` that either contains the `String` value for the given key, or an `ErrorType` detailing what went wrong.\n\nOne simple way to handle a `Result` is to deconstruct it using a `switch` statement.\n\n```swift\nswitch stringForKey(json, key: \"email\") {\n\ncase let .success(email):\n    print(\"The email is \\(email)\")\n    \ncase let .failure(.noSuchKey(key)):\n    print(\"\\(key) is not a valid key\")\n    \ncase .failure(.typeMismatch):\n    print(\"Didn't have the right type\")\n}\n```\n\nUsing a `switch` statement allows powerful pattern matching, and ensures all possible results are covered. Swift 2.0 offers new ways to deconstruct enums like the `if-case` statement, but be wary as such methods do not ensure errors are handled.\n\nOther methods available for processing `Result` are detailed in the [API documentation](http://cocoadocs.org/docsets/Result/).\n\n## Result vs. Throws\n\nSwift 2.0 introduces error handling via throwing and catching `Error`. `Result` accomplishes the same goal by encapsulating the result instead of hijacking control flow. The `Result` abstraction enables powerful functionality such as `map` and `flatMap`, making `Result` more composable than `throw`.\n\nSince dealing with APIs that throw is common, you can convert such functions into a `Result` by using the `materialize` method. Conversely, a `Result` can be used to throw an error by calling `dematerialize`.\n\n## Higher Order Functions\n\n`map` and `flatMap` operate the same as `Optional.map` and `Optional.flatMap` except they apply to `Result`.\n\n`map` transforms a `Result` into a `Result` of a new type. It does this by taking a function that transforms the `Value` type into a new value. This transformation is only applied in the case of a `success`. In the case of a `failure`, the associated error is re-wrapped in the new `Result`.\n\n```swift\n// transforms a Result\u003cInt, JSONError\u003e to a Result\u003cString, JSONError\u003e\nlet idResult = intForKey(json, key:\"id\").map { id in String(id) }\n```\n\nHere, the final result is either the id as a `String`, or carries over the `failure` from the previous result.\n\n`flatMap` is similar to `map` in that it transforms the `Result` into another `Result`. However, the function passed into `flatMap` must return a `Result`.\n\nAn in depth discussion of `map` and `flatMap` is beyond the scope of this documentation. If you would like a deeper understanding, read about functors and monads. This article is a good place to [start](http://www.javiersoto.me/post/106875422394).\n\n## Integration\n\n### Carthage\n\n1. Add this repository as a submodule and/or [add it to your Cartfile](https://github.com/Carthage/Carthage/blob/master/Documentation/Artifacts.md#cartfile) if you’re using [carthage](https://github.com/Carthage/Carthage/) to manage your dependencies.\n2. Drag `Result.xcodeproj` into your project or workspace.\n3. Link your target against `Result.framework`.\n4. Application targets should ensure that the framework gets copied into their application bundle. (Framework targets should instead require the application linking them to include Result.)\n\n### Cocoapods\n\n```ruby\npod 'Result', '~\u003e 5.0'\n```\n\n### Swift Package Manager\n\n```swift\n// swift-tools-version:4.0\nimport PackageDescription\n\nlet package = Package(\n    name: \"MyProject\",\n    targets: [],\n    dependencies: [\n        .package(url: \"https://github.com/antitypical/Result.git\",\n                 from: \"5.0.0\")\n    ]\n)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantitypical%2FResult","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fantitypical%2FResult","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantitypical%2FResult/lists"}