{"id":21365945,"url":"https://github.com/seanghay/result-of","last_synced_at":"2025-07-13T04:32:22.053Z","repository":{"id":41904889,"uuid":"290665575","full_name":"seanghay/result-of","owner":"seanghay","description":"A Kotlin utility sealed class for handling failure \u0026 success.","archived":false,"fork":false,"pushed_at":"2022-04-23T09:28:07.000Z","size":89,"stargazers_count":14,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2023-03-03T22:04:56.388Z","etag":null,"topics":["android","kotlin","sealed"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/seanghay.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-08-27T03:27:36.000Z","updated_at":"2022-10-21T03:34:07.000Z","dependencies_parsed_at":"2022-08-11T20:50:15.588Z","dependency_job_id":null,"html_url":"https://github.com/seanghay/result-of","commit_stats":null,"previous_names":[],"tags_count":null,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seanghay%2Fresult-of","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seanghay%2Fresult-of/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seanghay%2Fresult-of/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seanghay%2Fresult-of/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/seanghay","download_url":"https://codeload.github.com/seanghay/result-of/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225855953,"owners_count":17534967,"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":["android","kotlin","sealed"],"created_at":"2024-11-22T07:13:15.693Z","updated_at":"2024-11-22T07:13:16.335Z","avatar_url":"https://github.com/seanghay.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"## A utility sealed class for handling failure \u0026 success.\n\n[![Build](https://github.com/seanghay/result-of/workflows/Java%20CI/badge.svg)](https://travis-ci.org/seanghay/result-of)\n[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)\n\n\nThis is not the same as standard Kotlin `Result` because it is a **sealed class not a `value class` (previously called `inline class`)**.\n\n### Installation\n\n```groovy\ndependencies {\n    implementation(\"com.seanghay:resultof:1.0.0\")\n}\n```\n\n\n\u003e ⚠️ The library is still in `jcenter()`, I plan to move it to `mavenCentral()` soon. However, if you removed `jcenter()` from your repositories, it will not be resolved. I suggest use jitpack for the time being.\n\n\n#### JitPack\n\n```groovy\nallprojects {\n    repositories {\n        ...\n        maven { url 'https://jitpack.io' }\n    }\n}\n\ndependencies {\n    implementation 'com.github.seanghay:result-of:1.0.0'\n}\n```\n\n\n### Basic Usage\n\n```kotlin\nval result = resultOf {\n    fetchUsers()\n}\n\nresult.onSuccess { users -\u003e println(users) }\nresult.onFailure { throwable -\u003e println(throwable) }\n```\n\n### Usage with LiveData\n\n```kotlin\nclass MyViewModel : ViewModel() {\n    val profile = liveData {\n        val result = resultOf { userRepository.getMyProfile() }\n        emit(result)\n    }\n}\n```\n\n```kotlin\nclass MyFragment: Fragment() {\n    \n    private val viewModel: MyViewModel by viewModels()\n    \n    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {\n    \n        // observe user profile result\n        viewModel.profile.observe(viewLifecycleOwner) { result -\u003e \n            \n            // we only need `profileUrl` so we map it.\n            result.map { it.profileUrl }.onSuccess { url -\u003e\n                Glide.with(this)\n                    .load(url)\n                    .into(binding.imageView)\n            }\n\n            result.onFailure { throwable -\u003e \n                Toast.makeText(\n                    requireContext(), \n                    throwable?.localizedMessage ?: \"Oops!\", \n                    Toast.LENGTH_SHORT\n                ).show()\n            }\n        }\n    }\n}\n```\n\n### Transformations\n\n- `map` Map successful value into something else\n- `flatMap` Map another `ResultOf\u003cT\u003e` into anther `ResultOf\u003cR\u003e`\n- `failureMap` Map throwable of the Failure into another throwable. For example, map `IllegalStateException` to `MyException`\n- `failureFlatMap` same as `flatMap` but for Failure\n\n\n### Retrieving Value \u0026 Throwable\n\n- `result.valueOrNull`\n- `result.valueOrThrow`\n- `result.valueOrDefault { \"my-default-value\" }`\n- `result.throwableOrNull`\n\n\n### Conversion\n\nWe can convert from standard Kotlin Result into ResultOf by calling `asResultOf()` on Result object.\n\n```kotlin\nval result = runCatching { 12345 }.asResultOf()\n```\n\n\n### Nullable ResultOf to Non-null ResultOf Conversion\n\nMost of the time, we don't want `null` value to be the successful value, so we want it to be a Failure instead.\nWe can do that by calling `failureOnNull()`\n\n```kotlin\nval nullableResult: ResultOf\u003cString?\u003e = resultOf { null }\nval nonNullResult: ResultOf\u003cString\u003e = nullableResult.failureOnNull()\n\nprintln(nullableResult.isFailure)\n// false\n\nprintln(nonNullResult.isFailure)\n// true\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseanghay%2Fresult-of","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fseanghay%2Fresult-of","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseanghay%2Fresult-of/lists"}