iT邦幫忙

2022 iThome 鐵人賽

DAY 3
0
自我挑戰組

Kotlin 隨寫系列 第 3

Arrow KT - Either Monad

  • 分享至 

  • xImage
  •  
sealed class Either<out A, out B>

在寫程式過程中,function 會失敗是很常見而且需要正視的,像是連線的問題,Json parsing 等等。

為了要處理這些錯誤,常見的解決方法是丟出 exception. But ,就是這個 But,exception 是很難被編譯器追蹤,型化 (shape/form)。要知道是什麼錯誤,我們就必須要回頭看 source code. 也要確定這個 exeption 有被好好的處理、抓住。這樣當程式愈來愈大時就會更難處理。

Either Monad

Either 是由兩個 Type 所組合起來的,分別為 Right 以及 Left。 Right 就是 Right 所以就是 happy path, 左邊就是 sad path。利用 Either 就可以在寫程式或編譯期優雅的處理 error 或是 happy path

Yes


上一篇
Kotlin 的 Value Class ,再見了 type alias
系列文
Kotlin 隨寫3
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言