{"id":23712156,"url":"https://github.com/digillect/cl-result-and-error","last_synced_at":"2025-10-06T01:07:35.287Z","repository":{"id":64908433,"uuid":"578643695","full_name":"Digillect/cl-result-and-error","owner":"Digillect","description":"Result and Error types","archived":false,"fork":false,"pushed_at":"2023-01-07T19:35:11.000Z","size":24,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-12-30T19:57:17.803Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","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/Digillect.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-12-15T14:42:13.000Z","updated_at":"2022-12-15T20:44:14.000Z","dependencies_parsed_at":"2023-02-07T23:01:00.432Z","dependency_job_id":null,"html_url":"https://github.com/Digillect/cl-result-and-error","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Digillect%2Fcl-result-and-error","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Digillect%2Fcl-result-and-error/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Digillect%2Fcl-result-and-error/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Digillect%2Fcl-result-and-error/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Digillect","download_url":"https://codeload.github.com/Digillect/cl-result-and-error/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239800432,"owners_count":19699122,"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-12-30T19:57:24.022Z","updated_at":"2025-10-06T01:07:30.243Z","avatar_url":"https://github.com/Digillect.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Digillect Common Libraries: Result and Error\n\nImplementation of the `Result\u003cT\u003e` type that is used to return either successful\nor unsuccessful result from methods.\n\nInspired by Louthy's implementation of `Fin\u003cA\u003e`, but the `Error` does not contain\nany members and is solely used as a marker class.\n\n## Usage\n\n### Returning successful results\n\nTo return a successful result you can either call `Result.Success` passing in return value or\njust return this value, that will be converted into a `Result`.\n\n```csharp\nResult\u003cint\u003e Multiply(int v1, int v2) =\u003e Result.Success(v1 * v2);\n\nResult\u003cint\u003e Add(int v1, int v2) =\u003e v1 + v2;\n```\n\n### Returning errors\n\nThe common scenario imply creation of the specific `Error`-derived class(es) to report the\nproblem in the program flow, for example:\n\n```csharp\nclass DivisionByZeroError : Error\n{\n    public override string ToString() =\u003e \"Division by zero is prohibited.\";\n}\n\nResult\u003cdouble\u003e Divide(int numerator, int denominator)\n{\n    if (denominator == 0)\n    {\n        return new DivizionByZeroError();\n    }\n    \n    return numerator/denominator;\n} \n```\n\nIf you only need to report that the problem has happened (and provide an error message), a predefined\n`UnspecifiedError` class can be used either by creating and returning an instance of that class or by the calling\n`Result\u003cT\u003e.Error()` passing in the error message:\n\n```csharp\nResult\u003cdouble\u003e Divide(int numerator, int denominator)\n{\n    if (denominator == 0)\n    {\n        return Result\u003cdouble\u003e.Error(\"Division by zero is prohibited.\");\n    }\n \n    // Manually construct the result instead of casting, just for example.   \n    return Result.Success(numerator/denominator);\n}\n```\n\n### Handling results\n\nThere are many ways to deal with the result, but the main one is to use `Match` method and provide\neither functions or actions to handle the corresponding result:\n\n```csharp\nvoid DivideAndPrintResult(int v1, int v2)\n{\n    DivideTwoIntegers(v1, v2).Match(\n        result =\u003e Console.WriteLine($\"Division result is {result}\"),\n        error =\u003e Console.WriteLine($\"Error: {error}\"));\n}\n\ndouble DivideAndReturnZeroOnError(int v1, int v2)\n{\n    return Divide(v1, v2).Match(result =\u003e result, error =\u003e 0);\n}\n```\n\nThere are also shortcuts for the latter case:\n\n```csharp\ndouble DivideAndReturnZeroOnError(int v1, int v2) =\u003e Divide(v1, v2).IfFailure(0);\n```\n\n`Result` can be queried for the Success or Failure state by using `IsSuccess` and `IsFailure`. Sometimes,\nwhen you've already ensured that result is either successful or failed and need to access the corresponding\nvalue or an error you can cast that result to `T` or to `Error`. If the guard check has not been performed,\nand you query a value from the faulted result or an error from the successful result, one of the\n`ResultIsNotSuccessException` or `ResultIsNotFailureException` will be thrown.\n\nYou can also use the result in the `switch` expression by accessing the `Case` property:\n\n```csharp\ndouble DivideAndReturnZeroOnError(int v1, int v2) =\u003e Divide(v1, v2).Case switch {\n    double result =\u003e result,\n    Error error =\u003e 0,\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdigillect%2Fcl-result-and-error","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdigillect%2Fcl-result-and-error","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdigillect%2Fcl-result-and-error/lists"}