This is a simple standalone utility library for managing unsafe results in Kotlin instead of throwing exceptions.
import dev.razshare.result.Result
import dev.razshare.result.success
import dev.razshare.result.failure
data class User(val username: String, val password: String)
fun validateUser(user: User): Result<User> {
if(user.password.length < 4){
return failure("User password must be at least 4 characters, received ${user.password.length} characters.")
}
return success(user)
}
fun main(){
val (user, error) = validateUser(User(username = "foo", password = "bar")).unwrap()
if(error.isPresent){
println(error.get)
return
}
println("User ${user.username} is valid.")
}-
Add the repository to your
build.gradle.ktsrepositories { maven { url = uri("https://raw.githubusercontent.com/tncrazvan/artifacts/main") metadataSources { mavenPom() artifact() ignoreGradleMetadataRedirection() } } } -
Add the library to your
build.gradle.kts, for exampledev.razshare.resultdependencies { implementation("dev.razshare:result:0.1.0") }
-
Add the repository to your
pom.xml<repositories> <repository> <id>dev.razshare</id> <url>https://raw.githubusercontent.com/tncrazvan/artifacts/main</url> </repository> </repositories>
-
Add the library to your
pom.xml, for exampledev.razshare.result<dependencies> <dependency> <groupId>dev.razshare</groupId> <artifactId>result</artifactId> <version>0.1.0</version> <type>jar</type> </dependency> </dependencies>