I see <https://youtrack.jetbrains.com/issue/KT-793...
# javascript
p
I see https://youtrack.jetbrains.com/issue/KT-79394 is implemented in Kotlin 2.2.20-Beta2. Is there any examples how to use it?
a
I've just used it inside kotlinx.browser, you can take a look: https://github.com/Kotlin/kotlinx-browser/tree/master/src/webMain/kotlin/org.w3c The idea was quite simple: you can just move your declarations from
wasmJsMain
to
webMain
and it could be compiled to both wasmJs and JS targets (the only limination, that for declarations under kotlin.js package you need manually to add imports)
thank you color 1
๐Ÿ‘ 1
p
@Artem Kobzar thanks for great work! But i have one more question, if i'm not using applyDefaultHierarchyTemplate(), how should i define webMain and webTest source sets? Now i'm just creating them with such names and make depend on it from js and wasmJs targets souresets, but got compilation error for wasmJs target
Copy code
import kotlinx.browser.window
import org.w3c.dom.url.URLSearchParams

URLSearchParams(window.location.search) // Argument type mismatch: actual type is 'String', but 'JsAny?' was expected.
wasmJs sourceSet have also org.jetbrains.kotlinx:kotlinx-browser dependency js target compiles successfully
Solved! If i convert toJsString() it compiles ok for both targets
Copy code
URLSearchParams(window.location.search.toJsString())
a
Yes, you can. And the most valuable thing there that for JS target all the convertions to JsSomething and back are zero-cost abstractions (it just re-use the value itself)
thank you color 1
๐Ÿ”ฅ 1
t
Or you can use Kotlin Wrappers, where such conversions aren't required. ๐Ÿ˜‰
p
Thanks, @turansky! But i prefer kotlinx-browser cause it just externals for browser api.
t
cause it just externals for browser api
It's what kotlin-browser is ๐Ÿ˜‰ With 100% of stable API support.
p
Now i'm confused! Why there are two official JetBrains libraries then?
t
p
Thanks! But i still can't understand what breaking changes could be for kotlin-browser if it is just externals for stable browser api? By stable browser api i mean w3c standarts. If they will be changing then both libs should follow.
t
Short example:
Copy code
// non-strict 
external A(
   p: Any // String | Double | Boolean 
)

// strict 
external A(
   p: String 
) {
   constructor(p: Double)
   constructor(p: Boolean)
}
Let's deprecate constructor with
Boolean
parameter ๐Ÿ˜‰
Copy code
// non-strict 
// No breaking change
external A(
   p: Any // String | Double
)

// strict 
external A(
   p: String 
) {
   constructor(p: Double)
   // BREAKING CHANGE 
   // constructor(p: Boolean)
}
More strictness -> more breaking changes ๐Ÿ˜ž
p
Got it! Thanks for explanations!
t
And common
kotlin-browser
is already available. ๐Ÿ˜‰ You can use it with Kotlin
2.2.0
p
@turansky what about performance and code size comparison?
t
No difference expected in common case