Creating a class that implements an interface within the interface itself

huangapple go评论63阅读模式
英文:

Creating a class that implements an interface within the interface itself

问题

我在开发者.android的MarsUI Android教程中看到了这段代码:

sealed interface MarsUiState {
data class Success(val photos: List<MarsPhoto>) : MarsUiState
object Error : MarsUiState
object Loading : MarsUiState
}


看起来`Success`数据类实现了与其所在的接口相同的接口。这是如何可能的?感觉像是一些递归的魔法。难道类不需要接口首先存在吗?

我期望如果我需要这个接口,我会创建一个在接口外部实现它的类,或者将`::class.java`传递给一些其他函数,由它来创建该类。
英文:

I was looking at this code on the MarsUI android tutorial by developers.android:

sealed interface MarsUiState {
    data class Success(val photos: List&lt;MarsPhoto&gt;) : MarsUiState
    object Error : MarsUiState
    object Loading : MarsUiState
}

It looks like the Success data class implements the same interface it is inside of. How is that possible? It feels like some recursive magic. Doesn't the class need the interface to exist first?

I'd expect that if I need the interface, I would create a class that implements it outside of it or pass a ::class.java to some other function that would do the creation of that class.

答案1

得分: 2

How is that possible?
如何可能?

The Kotlin compiler allows it.
Kotlin编译器允许它。

It feels like some recursive magic. Doesn't the class need the interface to exist first?
感觉就像是一种递归魔法。类不需要首先存在接口吗?

No. The processing of the classes happens at compile time. Nothing "exists" at compile time. The compiler is validating syntax and, when everything checks out, creates the bytecode that will actually run.
不需要。类的处理发生在“编译”时间。在“编译”时,没有任何东西“存在”。编译器正在验证语法,当一切都检查通过时,它会创建实际运行的字节码。

Your code is just text. You and I, by looking at the text, can tell that an interface name MarsUiState is being declared and that a class Success implements that interface that was just declared. The fact that Success is "inside" MarsUiState is just a semantic detail.
你的代码只是文本。通过查看文本,你和我都可以看出正在声明一个接口名称MarsUiState,并且一个类Success实现了刚刚声明的接口。SuccessMarsUiState内部的事实只是语义细节。

That is, the creators of the language have decided that this is valid syntax for defining a sealed class hierarchy. They could have decided that you you needed to annotate the class, declare it in a separate file, or prefixed the class name with "BooYah!". As long as it followed some logic the compiler could parse.
也就是说,语言的创建者决定这是定义封闭类层次结构的有效语法。他们本可以决定你需要注释类,将其声明在单独的文件中,或者在类名前面加上“BooYah!”。只要它遵循一些编译器可以解析的逻辑。

Don't confuse declaring the class inside the interface as having an instance of the class within the interface (that would be recursive).
不要将在接口内部“声明”类与在接口内部“拥有”类的实例混淆(后者会是递归的)。

Similarly, the Kotlin compiler has been coded to detect that MarsUiState has been declared by the time it parses Success and the creators of the language have decided that this is valid syntax.
类似地,Kotlin编译器已经编码以在解析Success时检测到MarsUiState已被声明,并且语言的创建者决定这是有效的语法。

英文:

> How is that possible?

The Kotlin compiler allows it.

> It feels like some recursive magic. Doesn't the class need the interface to exist first?

No. The processing of the classes happens at compile time. Nothing "exists" at compile time. The compiler is validating syntax and, when everything checks out, creates the bytecode that will actually run.

Your code is just text. You and I, by looking at the text, can tell that an interface name MarsUiState is being declared and that a class Success implements that interface that was just declared. The fact that Success is "inside" MarsUiState is just a semantic detail.

That is, the creators of the language have decided that this is valid syntax for defining a sealed class hierarchy. They could have decided that you you needed to annotate the class, declare it in a separate file, or prefixed the class name with "BooYah!". As long as it followed some logic the compiler could parse.

Don't confuse declaring the class inside the interface as having an instance of the class within the interface (that would be recursive).

Similarly, the Kotlin compiler has been coded to detect that MarsUiState has been declared by the time it parses Success and the creators of the language have decided that this is valid syntax.

huangapple
  • 本文由 发表于 2023年4月10日 23:30:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978433.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定