Last active
November 25, 2020 08:55
-
-
Save yasukotelin/a0d255c546e0b10746e9461904a9f191 to your computer and use it in GitHub Desktop.
kotlin-result紹介用のスニペットたち
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main(args: Array<String>) { | |
when (val r1 = getUser()) { | |
is Result.Success -> { | |
when (val r2 = login(r1.data)) { | |
is Result.Success -> { | |
when (val r3 = setupSomething()) { | |
is Result.Success -> { | |
println("Success") | |
} | |
is Result.Error -> { | |
println("Failed") | |
} | |
} | |
} | |
is Error -> { | |
println("Failed") | |
} | |
} | |
} | |
is Error -> { | |
println("Failed") | |
} | |
} | |
} | |
fun getUser(): Result<User> { | |
return Result.Success(User("user name", "abcdefg")) | |
} | |
fun login(user: User): Result<Unit> { | |
return Result.Success(Unit) | |
} | |
fun setupSomething(): Result<Unit> { | |
return Result.Success(Unit) | |
} | |
sealed class Result<out R> { | |
data class Success<out T>(val data: T) : Result<T>() | |
data class Error(val exception: Exception) : Result<Nothing>() | |
} | |
data class User(val name: String, val token: String) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.github.michaelbull.result.Ok | |
import com.github.michaelbull.result.Result | |
import com.github.michaelbull.result.flatMap | |
import com.github.michaelbull.result.mapBoth | |
fun main() { | |
getUser() | |
.flatMap { login(it) } | |
.flatMap { setupSomething() } | |
.mapBoth( | |
success = { println("success") }, | |
failure = { println("failure") } | |
) | |
} | |
fun getUser(): Result<User, Error> { | |
return Ok(User(name = "user name", token = "abcdefg")) | |
} | |
fun login(user: User): Result<Unit, Error> { | |
return Ok(Unit) | |
} | |
fun setupSomething(): Result<Unit, Error> { | |
return Ok(Unit) | |
} | |
data class User(val name: String, val token: String) | |
object Error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let r = get_user() | |
.and_then(|u| login(&u)) | |
.and_then(|_| setup_something()); | |
match r { | |
Ok(_) => println!("Success login"), | |
Err(err) => println!("Login error: {}", err), | |
} | |
} | |
fn get_user() -> Result<User, String> { | |
Ok(User { | |
name: "User name".to_string(), | |
token: "abcdefg".to_string(), | |
}) | |
} | |
fn login(user: &User) -> Result<(), String> { | |
Ok(()) | |
} | |
fn setup_something() -> Result<(), String> { | |
Ok(()) | |
} | |
struct User { | |
name: String, | |
token: String, | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Main { | |
def main(args: Array[String]): Unit = { | |
val result = for { | |
user <- getUser() | |
_ <- login(user) | |
_ <- setupSomething() | |
} yield () | |
result match { | |
case Right(_) => println("Success") | |
case Left(e) => println(e) | |
} | |
} | |
def getUser(): Either[UserNotFoundException, User] = { | |
Right(User("User name", "abcdefg")) | |
} | |
def login(user: User): Either[LoginErrorException, Unit] = { | |
Right() | |
} | |
def setupSomething(): Either[SetupErrorException, Unit] = { | |
Right() | |
} | |
} | |
case class User(userName: String, token: String) | |
class UserNotFoundException extends RuntimeException | |
class LoginErrorException extends RuntimeException | |
class SetupErrorException extends RuntimeException |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment