kotlin - Kotlin/Native 中的 ${type}Var 是什么?

标签 kotlin kotlin-native

This documentation当它试图说出什么是 ${type}Var 时,我非常不清楚。

...for Kotlin enums it is named ${type}Var

什么?!什么是 Kotlin 枚举?常规 Kotlin 枚举?

enum class MyEnum {
    FIRST, SECOND
}

我认为这没有暗示。

好的,让我们看一下本文档中的示例:

struct S* is mapped to CPointer<S> , int8_t* is mapped to CPointer<int_8tVar>

好了,明白了

char** is mapped to CPointer<CPointerVar<ByteVar>>

为什么是char**映射到CPointer<CPointerVar<ByteVar>>但不是CPointer<CPointer<Byte>>

最后的问题是:IntVar 是什么? , LongVar , CPointerVar<T>和其他类似${type}Var的东西?

最佳答案

您应该仔细阅读整个段落。

All the supported C types have corresponding representations in Kotlin:

  • Enums can be mapped to Kotlin enum

C 中也有左值和右值(在 C++ 中,左值对应的是 Type &,右值对应的是 Type)。主要区别是左值可以设置为某个值,而右值在初始化后不能更改。因此,对于 C 中的每种类型,您都需要它自己的 Kotlin 类型来表示左值和右值。

在主题中

All the supported C types have corresponding representations in Kotlin:

仅考虑右值。 但对于左值,您唯一需要添加的是 Var到类型结束。唯一的异常(exception)是

For structs (and typedefs to structs) this representation is the main one and has the same name as the struct itself

现在让我们回到枚举。常规 Kotlin 枚举映射到常规 C 枚举。所以实际上FIRSTSECOND有类型 MyEnum两种语言。但是如果你想创建一个包含 MyEnum 的变量怎么办?例如:

// This is C Code
MyEnum a = FIRST;

a类型为MyEnum在 C 中,但它是左值(在 C++ 中是 MyEnum & ),所以在 Kotlin 中 a将具有类型 MyEnumVar因为这正是文档中所说的:${type}Var ,其中${type} = MyEnum .

对于下一个问题:

The type argument T of CPointer must be one of the "lvalue" types

所以对于struct S*应该是CPointer<SVar> ,但请记住struct s 是异常(exception),我们不应该添加 Var ,所以这只是 CPointer<S> .

  • int8_t*CPointer<int_8tVar> - 这里也不异常(exception)。
  • char*CPointer<ByteVar> - 同样也不异常(exception)(仅左值类型,结构除外)。
  • char**CPointer<CPointerVar<ByteVar>>因为我们需要 CPointer<ByteVar> 的左值这正是CPointerVar<ByteVar> .

最后: IntVar , LongVar , CPointerVar<T>和其他东西是类型 int 的左值, long , CPointer 。如果您想更改函数中的对象,可能需要这样做。类似 Ref<${type}>在Java中。

关于kotlin - Kotlin/Native 中的 ${type}Var 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55249331/

相关文章:

kotlin - 为什么 Kotlin native 可执行文件比等效的 Rust 可执行文件大?

tornadofx - 我如何将 TornadoFx 与 Kotlin/Native 结合使用

websocket - 使用 Kotlin 时是否有适用于 iOS 的 WebSocket 客户端库?

android - 如何从一个地方更改整个应用程序中的默认ClickSound

android - RxJava,如果我不调用 dispose 会发生什么?

android - 摆脱嵌套的任务类型

android - Kotlin-将ksoap2作为协程vs异步运行-哪个更好?

ios - 将 Kotlin 接口(interface)的 Swift 实现传递给 Kotlin 方法时出错

android - 如何在 Kotlin Native 中使用 kotlin.system?

java - 无法使用 Kotlin 在项目中设置 Realm