博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
kotlin_Kotlin的虚无本质
阅读量:2526 次
发布时间:2019-05-11

本文共 6172 字,大约阅读时间需要 20 分钟。

kotlin

by Adam Arold

亚当·阿罗德(Adam Arold)

Kotlin的虚无本质 (The Nature of Nothing in Kotlin)

For someone who comes from the Java world, the concept of Nothing might be confusing. In this article, I’ll try to clean that up with some practical examples to boot.

对于来自Java世界的人来说,Nothing的概念可能会令人困惑。 在本文中,我将尝试通过一些实际的示例进行引导来进行清理。

Simply put, Nothing is a :

简而言之, Nothing是 :

In type theory, a theory within mathematical logic, the bottom type is the type that has no values. A function whose return type is bottom cannot return any value.

在类型理论(一种数学逻辑内的理论)中,底部类型是没有值的类型。 返回类型为bottom的函数不能返回任何值。

So this means that Nothing is something like Void in Java, and the opposite of Any in Kotlin, no? Well, not quite.

因此,这意味着Java中Nothing什么比Void更好,与Kotlin中的Any相反,不是吗? 好吧,不完全是。

In Java, when a method has no return value, we can declare its return type as void:

在Java中,当一个方法没有返回值时,我们可以将其返回类型声明为void

In Kotlin, however, we already have a type which is returned from functions which don’t declare a return type: .

但是,在Kotlin中,我们已经有一个类型,该类型是从未声明返回类型的函数中返回的:

Although the documentation says it corresponds to void, there is an important difference. Unit has an actual instance — that’s why we can assign it to a variable, like in the example above.

尽管文档说它对应于void ,但还是有一个重要的区别。 Unit有一个实际的实例,这就是我们可以将其分配给变量的原因,就像上面的示例一样。

So what good does Nothing do for us? Let’s consider this function which has no return value:

那么,什么对我们没有Nothing ? 让我们考虑一下没有返回值的函数:

This function would have returned Unit if it hadn’t thrown an Exception. Now if you guess that this is exactly the case of returning Nothing, you are right. Let’s look at the actual implementation of it:

如果未抛出Exception则此函数将返回Unit 。 现在,如果您猜想这确实是返回Nothing的情况,那么您是对的。 让我们看一下它的实际实现:

Since it has a private constructor and it is not called from the class itself, this means that there can’t be any instances of Nothing. Nothing is also a bottom type, which means that it is a subtype of every other type (including Unit).

由于它具有私有构造函数,因此不会从类本身调用它,因此这意味着不存在Nothing任何实例。 Nothing也是底部类型,这意味着它是所有其他类型(包括Unit )的子类型。

信令与得天独厚的条件Nothing (Signaling exceptional conditions with Nothing)

Now you might begin to wonder why all this stuff is useful. Let’s say that we have a function which interoperates with Java classes which might return null:

现在您可能开始怀疑为什么所有这些东西都有用。 假设我们有一个与Java类互操作的函数,该类可能返回null

If we call this in our code, we have to do something about the nullability if we want a simple non-nullable String out of this:

如果在我们的代码中调用此方法,那么,如果要从中获得一个简单的不可为null的String ,就必须对可为性做一些事情:

But wait… This doesn’t compile because throwException returns Unit and we want to get a String. Let’s try redefining throwException in a more useful way:

但是,请稍候……这不会编译,因为throwException返回Unit并且我们想要一个String 。 让我们尝试以更有用的方式重新定义throwException

Our code now compiles. Why is that? The answer is that since Nothing is a subtype of every other class in Kotlin, the compiler can accept it, and because throwException is never going to return successfully, its assignment will never happen. Better yet now you can start using it to explicitly state that a function will always throw an Exception.

我们的代码现在可以编译了。 这是为什么? 答案是,由于Nothing是Kotlin中所有其他类的子类型,因此编译器可以接受它,并且因为throwException永远不会成功返回,所以它的赋值将永远不会发生。 更好的是,现在您可以开始使用它来明确声明函数将始终抛出Exception

Understanding how Nothing works might be a bit hard, but you can develop a nice intuition for it if you imagine Nothing as something which is the opposite of Any. Any is the supertype of everything (just like Object in Java) and Nothing is the subtype of everything.

理解Nothing工作原理可能会有些困难,但是如果您将Nothing视为Any的反义词,则可以为它建立一个很好的直觉。 Any是一切的超类型(就像Java中的Object一样), Nothing是一切的子类型。

代表null (Representing null)

Now that you know how Nothing works, you might start feeling warm and fuzzy…until you try writing a function like this:

既然您了解了Nothing工作原理,您可能会开始感到温暖和模糊……直到您尝试编写如下函数:

If you try this with the previous example, it no longer compiles. Why is that? Let’s see if there is a value which can be returned from a function which has the Nothing? return type:

如果您在前面的示例中尝试使用此方法,它将不再编译。 这是为什么? 让我们看看是否有一个可以从Nothing?的函数返回的值Nothing? 返回类型:

Yes. The only value which can be returned from this method is null. This also means that null has the type of Nothing?. Although this is hardly useful in our day-to-day work now at least we understand it.

是。 此方法唯一可以返回的值为null 。 这也意味着null的类型为Nothing? 。 尽管这对我们现在的日常工作几乎没有用处,但至少我们了解这一点。

代数数据类型 (Algebraic data types)

are sometimes very useful, and you might already be using them even if you don’t know it (a linked list, for example). In Kotlin, we can use Nothing if we want to implement them to great effect. Let’s take a look at a good example, Either:

有时非常有用,即使您不知道它,您也可能已经在使用它们(例如,链表)。 在Kotlin中,如果我们想实现很大的效果,就不能使用Nothing 。 让我们看一个好例子, Either

Either represents a value which might be either Left or Right. Left traditionally means an exceptional condition and Right usually is just a normal value. But Either can’t hold both values.

Either表示一个值,要么为LeftRight 。 传统上,“ Left表示特殊情况,“ Right通常只是正常值。 但是, Either都不能同时拥有这两个价值。

We represent this concept with Nothing. The right type of Left and the left type of Right is Nothing. We can then use Either, for example, as a return type instead of throwing exceptions:

我们用Nothing代表这个概念。 右侧的LeftRightNothing 。 然后,我们可以使用Either作为返回类型,而不是抛出异常:

Note that if you replace Nothing with Unit or any other type, it simply won’t compile. But here it represents what it was made for: nothingness.

请注意,如果将Nothing替换为Unit或任何其他类型,它将根本无法编译。 但是在这里它代表了它的目的:虚无。

Another fine example of using Nothing is a tree structure:

使用Nothing另一个很好的例子是树结构:

Here Nothing represents a missing left or right Node in a Tree.

在这里, Nothing表示Tree缺少的左右Node

结论 (Conclusion)

Although Nothing might not be intuitive at first, once we learn how it works it quickly becomes a useful asset in our toolkit. We can use it to indicate nothingness in data structures, to signal the inevitability of Exceptions being thrown or even null.

尽管起初Nothing不是直观的,但是一旦我们了解了它的工作原理,它很快就会成为我们工具包中的有用资产。 我们可以使用它来指示数据结构中的虚无,以信号抛出Exception的必然性,甚至抛出null

Now that we have all this under our belt let’s go forth and Kode on!

现在我们已经掌握了所有这些,让我们继续前进 ,继续Kode!

Thanks for reading! You can read more of my articles on .

谢谢阅读! 您可以在上阅读更多我的文章。

翻译自:

kotlin

转载地址:http://tnkzd.baihongyu.com/

你可能感兴趣的文章
Java 概述及安装使用
查看>>
helloworld
查看>>
港交所OMD-C对接笔记
查看>>
线程安全问题了解一下
查看>>
转:IPv4的地址真的用光了吗
查看>>
java rmi远程方法调用实例
查看>>
Linux设置环境变量小结
查看>>
syslog()用法
查看>>
Java 内存区域和GC机制
查看>>
STM32上不使用外部晶振,OSC_IN和OSC_OUT的接法
查看>>
设计模式六大原则
查看>>
android中的数据库操作
查看>>
当用updatepanel和scriptmanager时,弹出框
查看>>
如何破解百度云大文件下载限制
查看>>
冒泡排序
查看>>
react中<link>和<navlink>区别
查看>>
C# 生成随机数
查看>>
Psutil模块的应用
查看>>
session概述
查看>>
MATLAB 单变量函数一阶及N阶求导
查看>>