I'm trying to figure out how to represent an enum ...
# multiplatform
z
I'm trying to figure out how to represent an enum from C in my common source set. In C the enum I'm trying to represent has different values instead of just using ordinal. So the quick solution I did was this:
Copy code
// much shorter than actual, its like 100 entries long
public enum class AVPixelFormat(public val value: Int) {
    AV_PIX_FMT_NONE(-1),
    AV_PIX_FMT_YUV420P(0),
}
// then when I pass it to the native C function I pass the value field
It works but I don't really like it, especially cause kotlin enum would need every value assigned. in C it just adds to the previous value Other idea is
Copy code
// not typesafe but no need to have value field
object AVPixelFormat {
    const val AV_PIX_FMT_NONE = -1
    const val AV_PIX_FMT_YUV420P = 0
}
The other idea I had is
Copy code
@JvmInline
public value class AVPixelFormat2(public val value: Int) {
    public companion object {
        public val AV_PIX_FMT_NONE: AVPixelFormat2 = AVPixelFormat2(-1)
        public val AV_PIX_FMT_YUV420P: AVPixelFormat2 = AVPixelFormat2(0)
    }
}
Is there something else that might be better for this kind of use? I wanna keep it typesafe and efficient