galex
02/23/2019, 2:57 PMDico
02/23/2019, 3:45 PMSvyatoslav Kuzmich [JB]
02/23/2019, 3:55 PMSvyatoslav Kuzmich [JB]
02/23/2019, 3:58 PMDico
02/23/2019, 4:00 PMgalex
02/23/2019, 5:24 PMgalex
02/23/2019, 5:39 PM@Suppress("CAST_NEVER_SUCCEEDS")
fun ArrayBuffer?.asByteArray(): ByteArray? = this?.run { Int8Array(this) as ByteArray }
Svyatoslav Kuzmich [JB]
02/23/2019, 5:41 PMgalex
02/23/2019, 5:47 PMgalex
02/23/2019, 5:49 PMSvyatoslav Kuzmich [JB]
02/23/2019, 5:55 PMByteArray
and Int8Array
are the same things on runtime we can use:
fun ArrayBuffer?.asByteArray(): ByteArray? =
this?.run { Int8Array(this).unsafeCast<ByteArray>() }
This will eliminate unnecesary type check and we don't have to use @Suppress
Generated code for unsafeCast
would be:
return $receiver != null ? new Int8Array($receiver) : null;
compared to as
version:
var tmp$;
if ($receiver != null) {
var tmp$_0;
tmp$ = Kotlin.isByteArray(tmp$_0 = new Int8Array($receiver)) ? tmp$_0 : throwCCE();
}
else
tmp$ = null;
return tmp$;
Svyatoslav Kuzmich [JB]
02/23/2019, 6:06 PMI’d like that function to be part of kotlin/js by defaultCould you file a feature request at kotl.in/issue?
galex
02/23/2019, 6:13 PMgalex
02/23/2019, 6:13 PMgalex
02/23/2019, 6:14 PMSvyatoslav Kuzmich [JB]
02/23/2019, 6:22 PMunsafeCast
has downsides, if cast fails your program will fail the JavaScript way: usually farther down the execution with ambiguous error message 😁galex
02/23/2019, 6:23 PMgalex
02/23/2019, 9:00 PM