This documentation当它试图说出什么是 ${type}Var
时,我非常不清楚。
...for Kotlin enums it is named
${type}Var
什么?!什么是 Kotlin 枚举?常规 Kotlin 枚举?
enum class MyEnum {
FIRST, SECOND
}
我认为这没有暗示。
好的,让我们看一下本文档中的示例:
struct S*
is mapped toCPointer<S>
,int8_t*
is mapped toCPointer<int_8tVar>
好了,明白了
char**
is mapped toCPointer<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 枚举。所以实际上FIRST
和SECOND
有类型 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/