0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Kotlinでも使えるEitherとは

Posted at

はじめに

Arrowというライブラリを導入することでKotlinでもEitherを使うことができます

基本的な使い方

// Resultのように2種類の返却物をハンドリングできる
Either<Error, String>
either {
        // ensure は条件を満たさない場合に指定した Either.Left で処理を終了する
        ensure(isSuccess) { FugaError }
         // raise は 指定した Either.Left で処理を終了する
        raise(HogeError)
        // Any.right() は Either.Right(Any) と同じ意味
        "Success!".right() 
    }

// 返却された側はfoldでクロージャーを用いて処理できる
Any.fold(
    {
    it
    },
    { error ->
    error
    }
)

さいごに

Resultに慣れてる方が多いとは思いますが、Eitherを使ってみると意外と便利で読みやすいので一度試しに触ってみるのはありだと思います

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?