Skip to content

Add UnsafeResultValueAccess annotation to Result class for safer value access #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

hoc081098
Copy link

Issue #104

This pull request introduces a new @UnsafeResultValueAccess annotation to enforce safer access patterns for Result.value and Result.error in the kotlin-result library. Additionally, it updates the Kotlin conventions to opt into this annotation. Below are the key changes:

Enhancements to Result class safety:

  • Added the @UnsafeResultValueAccess annotation to mark access to Result.value and Result.error as potentially unsafe unless the Result state is explicitly checked using Result.isOk or Result.isErr. This encourages safer usage patterns. (kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Result.kt, [1] [2]

Kotlin conventions update:

  • Updated the Kotlin conventions in kotlin-conventions.gradle.kts to opt into the @UnsafeResultValueAccess annotation, ensuring that projects using this configuration can leverage the new safety mechanism. (buildSrc/src/main/kotlin/kotlin-conventions.gradle.kts, buildSrc/src/main/kotlin/kotlin-conventions.gradle.ktsR62)

@michaelbull
Copy link
Owner

This is a great idea! Can you show me what it looks like in IntelliJ when you try to use the .value and .error fields without explicitly adding the annotation? Does it cause a compilation error or is it a warning?

@hoc081098
Copy link
Author

This is a great idea! Can you show me what it looks like in IntelliJ when you try to use the .value and .error fields without explicitly adding the annotation? Does it cause a compilation error or is it a warning?

Yes 🙏

Screenshot 2025-04-28 at 22 07 47

And when running :example:compileKotlin gradle task:

value_or_error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants