{"id":48593132,"url":"https://github.com/diareuse/superposition","last_synced_at":"2026-04-08T20:53:22.442Z","repository":{"id":342308015,"uuid":"1173423293","full_name":"diareuse/superposition","owner":"diareuse","description":"Kotlin Multiplatform wrappers for Defensive Programming","archived":false,"fork":false,"pushed_at":"2026-03-25T10:04:11.000Z","size":172,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2026-04-08T20:53:21.918Z","etag":null,"topics":["defensive-programing","kotlin","kotlin-multiplatform","tool"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/diareuse.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,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-03-05T10:53:50.000Z","updated_at":"2026-03-25T10:03:51.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/diareuse/superposition","commit_stats":null,"previous_names":["diareuse/superposition"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/diareuse/superposition","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diareuse%2Fsuperposition","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diareuse%2Fsuperposition/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diareuse%2Fsuperposition/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diareuse%2Fsuperposition/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/diareuse","download_url":"https://codeload.github.com/diareuse/superposition/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diareuse%2Fsuperposition/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31573788,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-08T14:31:17.711Z","status":"ssl_error","status_checked_at":"2026-04-08T14:31:17.202Z","response_time":54,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["defensive-programing","kotlin","kotlin-multiplatform","tool"],"created_at":"2026-04-08T20:53:20.523Z","updated_at":"2026-04-08T20:53:22.427Z","avatar_url":"https://github.com/diareuse.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Superposition\n\n**Superposition** is a lightweight, high-performance `Result` type for Kotlin. It leverages Kotlin's `value class` to\nminimize object allocation overhead while providing a robust, functional approach to strictly typed error handling.\n\n## Features\n\n* 🚀 **Zero Overhead**: Implemented as a `@JvmInline value class`.\n* 🔒 **Type-Safe Failures**: Define arbitrary Failure domain objects, not limited to `Throwable`.\n* 🛑 **Explicit Handling**: The API enforces handling failure cases before accessing success values.\n* 🔗 **Functional Composition**: Includes standard operators like `map` and `recover`.\n* 🍬 **Syntactic Sugar**: Operator overloading for concise syntax.\n\n## Implementation\n\n![Maven Central Version](https://img.shields.io/maven-central/v/io.github.diareuse/superposition-core)\n\n```kotlin\ndependencies {\n    implementation(\"io.github.diareuse:superposition-core:\u003clatest-version\u003e\")\n}\n```\n\n## Usage\n\n### 1. The Core Concept\n\nThe core type is `Superposition\u003cSuccess, Failure\u003e`. To access the `Success` value, you must use `handle`. The\n`onFailure` block returns `Nothing`, guaranteeing flow interruption on error.\n\n```kotlin\nfun processData(result: Superposition\u003cString, ApiError\u003e): String {\n    // Access is only possible via handle\n    val value = result.handle { error -\u003e\n        // Must return Nothing (throw, return, etc.)\n        println(\"Error: ${error.code}\")\n        return \"Early exit\" \n    }\n    \n    // 'value' is guaranteed to be String here\n    return value\n}\n```\n\n### 2. Creating Superpositions\n\nWrap throwing code using the `superposition` builder and an `Unwrap` strategy to map exceptions to domain failures.\n\n```kotlin\nsealed interface MyDomainError {\n    data object DomainRuleBreak1 : MyDomainError\n    class Unknown(error: Throwable) : UnhandledException(error), MyDomainError\n    companion object : Superposition.Unwrap\u003cMyDomainError\u003e {\n        override fun map(error: Throwable) = when(error) {\n            is RuntimeException -\u003e DomainRuleBreak1\n            else -\u003e Unknown\n        }    \n    }\n}\n\nval result: Superposition\u003cString, MyDomainError\u003e = superposition(MyDomainError) {\n    if (someCondition) throw RuntimeException(\"Boom\")\n    \"Success Data\"\n}\n```\n\n### 3. Concise Syntax\n\n**The `invoke` operator**  \nShortcut for `handle`:\n\n```kotlin\nval value = result { error -\u003e return \"Failed\" }\n```\n\n**Guaranteed Success**  \nIf the Failure type is `Nothing`, you can unwrap directly:\n```kotlin\nval successOnly: Superposition\u003cString, Nothing\u003e = ...\nval value = successOnly() // No lambda required\n```\n\n### 4. Transformation \u0026 Recovery\n\n**Map**  \nTransform success values while preserving failure types:\n```kotlin\nval intResult = result.map { str -\u003e str.length }\n```\n\n**Flat Map**  \nTransform success values while preserving failure types:\n```kotlin\nval fooBar: (String) -\u003e Superposition\u003cInt, ...\u003e = { ... }\nval result = result.flatMap { str -\u003e fooBar(str) }\n```\n\n**Recover**  \nProvide a fallback value instead of interrupting flow:\n```kotlin\nval value = result.recover { failure -\u003e \"Fallback: ${failure.msg}\" }\n```\n\n### 5. Composition\n\nChain operations using the `superposition` scope.\n\n**Sequential unwrap**\n```kotlin\nval result = superposition(MyDomainError) {\n    // Unwrap results sequentially\n    val user = getUser().handle { throw UserNotFound(it) }\n    val service = getLoadBalancedService().handle { throw ServiceUnavailable(it) }\n    getProfile(user, service) // Returns the final result\n}\n```\n\n**Flattened unwrap**\n```kotlin\nval result = flatSuperposition(MyDomainError) {\n    val user = getUser().handle { throw UserNotFound(it) }\n    getProfile(user) // assumes -\u003e getProfile(): Superposition\u003c??, MyDomainError\u003e \n}\n```\n\n### 6. Debugging\n\nThe library includes `UnhandledException` which prints stack traces to `System.err` for unhandled exceptions during\ncreation or mapping, aiding in development debugging.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdiareuse%2Fsuperposition","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdiareuse%2Fsuperposition","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdiareuse%2Fsuperposition/lists"}