2

The following code does not compile in Kotlin Js:

"My String".toByteArray()

Any ideas why?

The error returned is:

[INFO]
[INFO] --- kotlin-maven-plugin:1.1.51:js (compile-js) @ client ---
[INFO] Kotlin version 1.1.51 (JRE 1.8.0_144-b01)
[INFO] Compiling Kotlin sources from [C:\myproject\src\main\kotlin]
[ERROR] C:\myproject\src\main\kotlin\mypackage\MyFile.kt: (15, 48) Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public fun Array<out Byte>.toByteArray(): ByteArray defined in kotlin.collections
public fun Collection<Byte>.toByteArray(): ByteArray defined in kotlin.collections
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:

The same code compiles in the non-javascript Kotlin compilation:

[INFO] --- kotlin-maven-plugin:1.1.51:compile (compile) @ client ---
[INFO] Kotlin version 1.1.51 (JRE 1.8.0_144-b01)
[INFO] Compiling Kotlin sources from [C:\myproject\src\main\kotlin, C:\myproject\src\main\java]
[INFO] Module name is client
[INFO]
auser
  • 6,307
  • 13
  • 41
  • 63
  • `String.toByteArray()` is from `kotlin-stdlib`/`kotlin-text`. Have a included a reference on that? According to this page: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/to-byte-array.html – cramopy Oct 21 '17 at 11:37

1 Answers1

3

String.toByteArray function is not provided in the Kotlin/JS standard library.

The documentation of that function has a section about Platform and version requirements. JVM there means that it's only available on Kotlin/JVM.

Ilya
  • 21,871
  • 8
  • 73
  • 92