{"id":17160490,"url":"https://github.com/mattpolzin/mayberequired","last_synced_at":"2025-09-29T07:59:13.362Z","repository":{"id":149417086,"uuid":"96604112","full_name":"mattpolzin/MaybeRequired","owner":"mattpolzin","description":"This library introduces the Required and MaybeRequired types as companions to the built-in Optional type","archived":false,"fork":false,"pushed_at":"2018-11-03T07:39:14.000Z","size":36,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-09-23T17:58:13.732Z","etag":null,"topics":["functional-programming","swift","swift-framework"],"latest_commit_sha":null,"homepage":"","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/mattpolzin.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,"zenodo":null}},"created_at":"2017-07-08T07:44:37.000Z","updated_at":"2019-07-28T14:51:22.000Z","dependencies_parsed_at":null,"dependency_job_id":"fb37b2d8-3684-4f80-a675-4b73c8bc7641","html_url":"https://github.com/mattpolzin/MaybeRequired","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/mattpolzin/MaybeRequired","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattpolzin%2FMaybeRequired","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattpolzin%2FMaybeRequired/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattpolzin%2FMaybeRequired/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattpolzin%2FMaybeRequired/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mattpolzin","download_url":"https://codeload.github.com/mattpolzin/MaybeRequired/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattpolzin%2FMaybeRequired/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":277483321,"owners_count":25825561,"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-29T02:00:09.175Z","response_time":84,"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":["functional-programming","swift","swift-framework"],"created_at":"2024-10-14T22:25:02.522Z","updated_at":"2025-09-29T07:59:13.356Z","avatar_url":"https://github.com/mattpolzin.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MaybeRequired\n[![MIT license](http://img.shields.io/badge/license-MIT-lightgrey.svg)](http://opensource.org/licenses/MIT) [![Swift 4.2](http://img.shields.io/badge/Swift-4.2-blue.svg)](https://swift.org) [![Build Status](https://app.bitrise.io/app/06779980531377cd/status.svg?token=W0a5g6rOZJhvt9UHzzZ4xA\u0026branch=master)](https://app.bitrise.io/app/06779980531377cd)\n\n## Motivation\nThis library introduces the `Required` type as a companion to the built-in `Optional` type. The concept of `Required` values is useful for encoding into a type whether values retrieved from a server API are \"optional\" (i.e. set or not) or \"required\" (i.e. necessarily set, but maybe not available to the client because of a server error, network error, user error, or client programmer error). Any piece of code operating on a `Required` value knows that the absense of the wrapped value represents an error. \n\n## Alternatives Compared\nThis is essentially a specialization of the popular `Result` type, but `MaybeRequired` makes it effortless to map between `Optional` and `Required` types without needing to represent `Optional`'s `.none` case as an `Error` type. Therefore, it is even easier to draw a parallel between `MaybeRequired` and another popular type, `Either`. \n\nIn fact, it would have been very easy to write the `MaybeRequired` type as `Either\u003cRequired, Optional\u003e`. This exercise was never performed so I cannot speak to whether it would have yielded elegant code or not, but it almost certainly would not have made the result easier to work with in practice.\n\n## In Brief\nJust as an `Optional` value can be `.some(value)` or `.none`, a `Required` value can be `.some(value)` or `.missing`. Note that, although `Optional.some(value)` and `Required.some(value)` can be compared to determine if the two `value`s are equal, `Optional.none` is not equal to `Required.missing`.\n\n`Optional\u003cWrapped\u003e` and `Required\u003cWrapped\u003e` values can both be mapped to functions that accept their `Wrapped` type and return `Optional\u003cNewWrapped\u003e` using the `suppose()` and `require()` methods.\n\nThink of `value1.suppose(value2function)` as \"Given that `value1` is `.some`, suppose the result of `value2function` is `.some`.\" If the result of `value2function` is `.none`, no big deal, it was only a supposition.\n\nThink of `value1.require(value2function)` as \"Given that `value1` is `.some`, require the result of `value2function` to be `.some`.\" If the result of `value2function` is `.none`, that is a big deal because it was required.\n\nA chain starting with an `Optional` value and containing only `suppose`s will result in an `Optional` value.\n\nA chain starting with a `Required` value and containing only `require`s will result in a `Required` value.\n\nChains starting with `Optional` values and containing any `require`s or starting with `Required` values and containing any `suppose`s will result in a `MaybeRequired` value.\n\n`MaybeRequired` values can be `.some(value)`, `.none`, or `.missing`. This type also offers comparison with `Optional` and `Required` and provides `suppose()` and `require()` methods.\n\n## A Taste\nSee `MaybeRequired/Example.swift` for a slightly more contextualized but also less concise example.\n\nHere is some anecdotal pseudocode:\n```\n// To state that a Person is Required\nlet requiredPerson: Requried\u003cPerson\u003e = Required(person)\n\n// To state that an Optional\u003cPerson\u003e is Required\nlet requiredPerson: Required\u003cPerson\u003e = Required(fromOptional: person)\n```\n```\n// Given a Required\u003cPerson\u003e (with an ID of some kind), we require that they have a name.\nlet name: Required\u003cString\u003e = person.require { names[$0.id] }\n\n// If we do not have the required person, the value will be .missing\n\n// If we do have the required person and the person does have a name, the value will be .some(String)\n\n// If we do have the required person and the person does not have a required name, the value will be .missing \n```\n```\n// Given an Optional\u003cDog\u003e (with an ID of some kind), we suppose that it has a name tag with a name on it.\nlet nameTag: String? = dog.suppose { dogTags[$0.id] }\n\n// If we do not have the optional dog, the value will be .none\n\n// If we do have the optional dog and the dog does have a name tag, the value will be .some(String)\n\n// If we do have the optional dog and the dog does not have the optional name tag, the value will be .none\n```\n```\n// Given a Required\u003cPerson\u003e, we suppose that they have a dog.\nlet dog: MaybeRequired\u003cDog\u003e = person.suppose { dogs[$0.id] }\n\n// If we do not have the required person, the value will be .missing\n\n// If we do have the required person and the person does have a dog, the value will be .some(Dog)\n\n// If we do have the required person but the person does not have the optional dog, the value will be .none\n```\n```\n// Given a Required\u003cPerson\u003e, we suppose that they have a dog.\n// Given that they have a dog, we suppose the dog has a name tag.\n// Given that the dog has a name tag, we require that the name tag is registered.\nlet registration: MaybeRequired\u003cRegistration\u003e = person.suppose { dogs[$0.id] }.suppose { dogTags[$0.id] }.require { reg[$0] }\n\n// If we do not have the required person, the value will be .missing\n\n// Otherwise, if we do not have the optional dog, the value will be .none\n\n// Otherwise, if we do not have the optional name tag, the value will be .none\n\n// Otherwise, if we do not have the required registration, the value will be .missing\n\n// Otherwise, the value will be .some(Registration)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattpolzin%2Fmayberequired","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmattpolzin%2Fmayberequired","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattpolzin%2Fmayberequired/lists"}