我在Arrow图书馆看了看,发现了here个.为什么要使用Option类型而不是Kotlin内置的nullable呢?

推荐答案

Disclaimer:如果你真的想详细地谈谈为什么Arrow是有用的,那么请go https://soundcloud.com/user-38099918/arrow-functional-library,听听从事这项工作的人的意见.(5:35min)

创建和使用该库的人想要以不同的方式使用Kotlin,而不是创建它并使用"the Option datatype similar to how Scala, Haskell and other FP languages handle optional values"的人.

This is just another way of defining return types of values that you do not know the output of.

Let me show you three versions:

nullability in Kotlin

val someString: String? = if (condition) "String" else null

object with another value

val someString: String = if (condition) "String" else ""

the Arrow version

val someString: Option<String> = if (condition) Some("String") else None

Kotlin逻辑的一个主要部分可能是永远不使用nullable types(如String?),但是在与Java交互时需要使用它.当这样做时,您需要使用安全呼叫,如string?.split("a")not-null assertion string!!.split("a").

I think it is perfectly valid to use safe calls when using Java libraries, but the Arrow guys seem to think different and want to use their logic all the time.

使用箭头逻辑的好处是"empowering users to define pure FP apps and libraries built atop higher order abstractions. Use the below list to learn more about Λrrow's main features".

Kotlin相关问答推荐

如何在Kotlin的嵌套作用域中解析对此的引用

如何编写带有依赖项的自定义Kotlin串行化程序?

相当于roomdb中的DateTime Bigint列的是什么

在 Kotlin 中实现并输入 Either

在 kotlin 原始字符串中转义三重引号

修改器的属性是什么,我需要更改以使角变圆且宽度更小?喷气背包组合

在 kotlin 中重载函数时,我在一些非常基本的代码上不断收到类型不匹配

Kotlin 具体类从抽象类和接口扩展而来,接口使用抽象类中实现的方法

Kotlin 日期格式从一种更改为另一种

Kotlin:如何使用第一个参数的默认值进行函数调用并为第二个参数传递一个值?

类型不匹配:Required: Context, Found: Intent

Kotlin 中的 Java Scanner 相当于什么?

防止导航到同一个片段

使用kotlinx协同程序测试中的类时出错

Kotlin使用运行时断言进行空判断?

在 suspendCoroutine 块中调用挂起函数的适当方法是什么?

无法解决:androidx.lifecycle:lifecycle-extensions-ktx:2.0.0-alpha1

Kotlin替换字符串中的多个单词

使用 java lambda 调用 kotlin 函数时,Kotlin 无法访问 kotlin.jvm.functions.Function1

如何在 spring-boot Web 客户端中发送请求正文?