https://kotlinlang.org logo
#kotlin-native
Title
# kotlin-native
d

David Herman

02/27/2023, 8:39 PM
I'm trying to write some code using
termios
in K/N.
termios
is a struct which has a bitflag field called
c_lflag
; its type is
platform.posix.tcflag_t
which appears to be an int on mac and a uint on linux. I'm trying to do some bit logic like this:
newTermios.c_lflag.and((_ECHO_.or(_ICANON_.or(_IEXTEN_))).inv())
(equivalent to
c_lflag & ~(ECHO | ICNON | IEXTEN)
in C). In order to support both targets, I thought
ECHO.or(...) as tcflag_t
would work, but I'm getting a warning that the cast will never succeed. What's the right solution here?
(I'm pretty sure I could use expect / actual for this case, but that seems like overkill)
It seems to me like I need to call
.toUInt()
on Linux and not on Mac
e

ephemient

02/27/2023, 8:46 PM
use
.convert()
d

David Herman

02/27/2023, 8:46 PM
Ah, let me try. Thanks!
Looks good so far, thank you, and sorry for the beginner question. (I was originally searching for a function like
cast
)
e

ephemient

02/27/2023, 8:50 PM
no worries. there's clearly room for improvement in the docs https://kotlinlang.org/api/latest/jvm/stdlib/kotlinx.cinterop/convert.html
l

Landry Norris

02/27/2023, 9:08 PM
Kotlin doesn't do the modifying casts you see with primitives in other languages (same issue casting int to double). You need the conversion methods, like toDouble. Convert is similar, but it checks the plaform type to convert to.
e

ephemient

02/27/2023, 9:10 PM
not to confuse things too much, but
Copy code
(fun (x: Double) { println(x) } as (Number) -> Unit).invoke(1 as Int)
works 😛
(that is a bug in Kotlin/JVM though)