https://kotlinlang.org logo
Join the conversationJoin Slack
Channels
100daysofcode
100daysofkotlin
100daysofkotlin-2021
advent-of-code
aem
ai
alexa
algeria
algolialibraries
amsterdam
android
android-architecture
android-databinding
android-studio
androidgithubprojects
androidthings
androidx
androidx-xprocessing
anime
anko
announcements
apollo-kotlin
appintro
arabic
argentina
arkenv
arksemdevteam
armenia
arrow
arrow-contributors
arrow-meta
ass
atlanta
atm17
atrium
austin
australia
austria
awesome-kotlin
ballast
bangladesh
barcelona
bayarea
bazel
beepiz-libraries
belgium
berlin
big-data
books
boston
brazil
brikk
budapest
build
build-tools
bulgaria
bydgoszcz
cambodia
canada
carrat
carrat-dev
carrat-feed
chicago
chile
china
chucker
cincinnati-user-group
cli
clikt
cloudfoundry
cn
cobalt
code-coverage
codeforces
codemash-precompiler
codereview
codingame
codingconventions
coimbatore
collaborations
colombia
colorado
communities
competitive-programming
competitivecoding
compiler
compose
compose-android
compose-desktop
compose-hiring
compose-ios
compose-mp
compose-ui-showcase
compose-wear
compose-web
connect-audit-events
corda
cork
coroutines
couchbase
coursera
croatia
cryptography
cscenter-course-2016
cucumber-bdd
cyprus
czech
dagger
data2viz
databinding
datascience
dckotlin
debugging
decompose
decouple
denmark
deprecated
detekt
detekt-hint
dev-core
dfw
docs-revamped
dokka
domain-driven-design
doodle
dsl
dublin
dutch
eap
eclipse
ecuador
edinburgh
education
effective-kotlin
effectivekotlin
emacs
embedded-kotlin
estatik
event21-community-content
events
exposed
failgood
fb-internal-demo
feed
firebase
flow
fluid-libraries
forkhandles
forum
fosdem
fp-in-kotlin
framework-elide
freenode
french
fritz2
fuchsia
functional
funktionale
gamedev
ge-kotlin
general-advice
georgia
geospatial
german-lang
getting-started
github-workflows-kt
glance
godot-kotlin
google-io
gradle
graphic
graphkool
graphql
graphql-kotlin
graviton-browser
greece
grpc
gsoc
gui
hackathons
hacktoberfest
hamburg
hamkrest
helios
helsinki
hexagon
hibernate
hikari-cp
hire-me
hiring
hongkong
hoplite
http4k
hungary
hyderabad
image-processing
india
indonesia
inkremental
intellij
intellij-plugins
intellij-tricks
internships
introduce-yourself
io
ios
iran
israel
istanbulcoders
italian
jackson-kotlin
jadx
japanese
jasync-sql
java-to-kotlin-refactoring
javadevelopers
javafx
javalin
javascript
jdbi
jhipster-kotlin
jobsworldwide
jpa
jshdq
juul-libraries
jvm-ir-backend-feedback
jxadapter
k2-early-adopters
kaal
kafka
kakao
kalasim
kapt
karachi
karg
karlsruhe
kash_shell
kaskade
kbuild
kdbc
kgen-doc-tools
kgraphql
kinta
klaxon
klock
kloudformation
kmdc
kmm-español
kmongo
knbt
knote
koalaql
koans
kobalt
kobweb
kodein
kodex
kohesive
koin
koin-dev
komapper
kondor-json
kong
kontent
kontributors
korau
korean
korge
korim
korio
korlibs
korte
kotest
kotest-contributors
kotless
kotlick
kotlin-asia
kotlin-beam
kotlin-by-example
kotlin-csv
kotlin-data-storage
kotlin-foundation
kotlin-fuel
kotlin-in-action
kotlin-inject
kotlin-latam
kotlin-logging
kotlin-multiplatform-contest
kotlin-mumbai
kotlin-native
kotlin-pakistan
kotlin-plugin
kotlin-pune
kotlin-roadmap
kotlin-samples
kotlin-sap
kotlin-serbia
kotlin-spark
kotlin-szeged
kotlin-website
kotlinacademy
kotlinbot
kotlinconf
kotlindl
kotlinforbeginners
kotlingforbeginners
kotlinlondon
kotlinmad
kotlinprogrammers
kotlinsu
kotlintest
kotlintest-devs
kotlintlv
kotlinultimatechallenge
kotlinx-datetime
kotlinx-files
kotlinx-html
kotrix
kotson
kovenant
kprompt
kraph
krawler
kroto-plus
ksp
ktcc
ktfmt
ktlint
ktor
ktp
kubed
kug-leads
kug-torino
kvision
kweb
lambdaworld_cadiz
lanark
language-evolution
language-proposals
latvia
leakcanary
leedskotlinusergroup
lets-have-fun
libgdx
libkgd
library-development
linkeddata
lithuania
london
losangeles
lottie
love
lychee
macedonia
machinelearningbawas
madrid
malaysia
mathematics
meetkotlin
memes
meta
metro-detroit
mexico
miami
micronaut
minnesota
minutest
mirror
mockk
moko
moldova
monsterpuzzle
montreal
moonbean
morocco
motionlayout
mpapt
mu
multiplatform
mumbai
munich
mvikotlin
mvrx
myndocs-oauth2-server
naming
navigation-architecture-component
nepal
new-mexico
new-zealand
newname
nigeria
nodejs
norway
npm-publish
nyc
oceania
ohio-kotlin-users
oldenburg
oolong
opensource
orbit-mvi
osgi
otpisani
package-search
pakistan
panamá
pattern-matching
pbandk
pdx
peru
philippines
phoenix
pinoy
pocketgitclient
polish
popkorn
portugal
practical-functional-programming
proguard
prozis-android-backup
pyhsikal
python
python-contributors
quasar
random
re
react
reaktive
realm
realworldkotlin
reductor
reduks
redux
redux-kotlin
refactoring-to-kotlin
reflect
refreshversions
reports
result
rethink
revolver
rhein-main
rocksdb
romania
room
rpi-pico
rsocket
russian
russian_feed
russian-kotlinasfirst
rx
rxjava
san-diego
science
scotland
scrcast
scrimage
script
scripting
seattle
serialization
server
sg-user-group
singapore
skia-wasm-interop-temp
skrape-it
slovak
snake
sofl-user-group
southafrica
spacemacs
spain
spanish
speaking
spek
spin
splitties
spotify-mobius
spring
spring-security
squarelibraries
stackoverflow
stacks
stayhungrystayfoolish
stdlib
stlouis
strife-discord-lib
strikt
students
stuttgart
sudan
swagger-gradle-codegen
swarm
sweden
swing
swiss-user-group
switzerland
talking-kotlin
tallinn
tampa
teamcity
tegal
tempe
tensorflow
terminal
test
testing
testtestest
texas
tgbotapi
thailand
tornadofx
touchlab-tools
training
tricity-kotlin-user-group
trójmiasto
truth
tunisia
turkey
turkiye
twitter-feed
uae
udacityindia
uk
ukrainian
uniflow
unkonf
uruguay
utah
uuid
vancouver
vankotlin
vertx
videos
vienna
vietnam
vim
vkug
vuejs
web-mpp
webassembly
webrtc
wimix_sentry
wwdc
zircon
Powered by Linen
javascript
  • h

    hallvard

    01/20/2019, 9:25 PM
    Hello, folks. In a library I'm working on, I have a class holding a map, and
    toString()
    on that class should return
    JSON.stringify(theMap)
    . But that won't work since js maps have this prototype thing in them (at least I think that's why), and so there are circular references in js runtime. Has anyone got a good idea about how I could convert the map into something that
    JSON.stringify()
    would like?
    r
    • 2
    • 4
  • h

    hallvard

    01/21/2019, 1:12 PM
    When transpiling to javascript, what does the kotlin compiler do to
    vararg
    function parameters? In a function of mine, there is only one parameter, and it is defined as
    vararg tags: String
    . Inside the function, I collect the vararg into a list:
    tags.asList()
    . In the produced js, the parameter is of course named
    tags
    , and I find this in the function body:
    asList(tags)
    . But wait... This takes the first string passed in and turns it into a character list.
    s
    n
    • 3
    • 2
  • g

    galex

    01/21/2019, 4:59 PM
    So with something like this: <Button variant=“contained” color=“primary”> Hello World </Button> What should I write to be able to use this via kotlin?
    s
    • 2
    • 5
  • s

    spierce7

    01/21/2019, 5:21 PM
    is the kotlin-dce-js plugin compatible with the kotlin-multiplatform plugin?
    d
    • 2
    • 1
  • a

    ankushg

    01/23/2019, 12:00 AM
    Is there a simple way to generate converters to/from `data class`es and Kotlin/JS's
    Json
    interface? https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.js/-json/index.html Is this something that
    kotlinx.serialization
    supports? I'm trying to create a wrapper around my Kotlin library so that I can pass in JS objects (i.e.,
    {foo: "bar"}
    and then convert the resulting
    Json
    object into an instance of
    data class MyDataClass(val foo: String)
    , and vice versa when returning an instance of
    MyDataClass
    from Kotlin to JS
    g
    • 2
    • 2
  • h

    hallvard

    01/25/2019, 3:32 PM
    I have a map with (among other things) a long value. Using the toString() method on the map, the long value comes out thus:
    time=1548429717649
    . But I am generating JSON, and am converting my map to an object before doing
    JSON.stringify()
    in order to avoid cyclic references from the map, and I see the long values represented as objects, and in the final JSON, they are represented thus:
    "time":{"low_":-2053476207,"high_":360}
    . I guess this has to do with dealing with 64bit numbers in javascript, but my question is: How do I detect this and how do I convert them? Using
    typeof
    as I pull them out of my map, I only get
    object
    ...
    g
    s
    • 3
    • 11
  • a

    Alexander

    01/27/2019, 11:32 AM
    Why is
    args
    of
    main
    function in kotlin/js always empty when running on nodejs?
    fun main(args: Array<String>) = println(args.size)
    // always prints 0
    To access command line arguments I should do something like:
    @JsModule("process")
    external object process {
        val argv: Array<String>
    }
    fun main() = println(process.argv.sliceArray(2 until process.argv.size))
    Not a super convenient way. And it is defferent from the other platforms.
    g
    • 2
    • 1
  • n

    natpryce

    01/29/2019, 8:51 AM
    Q: At the moment, user-defined types cannot extend function types when compiling to the JS platform. Are there plans to remove that limitation?
    b
    j
    • 3
    • 8
  • h

    hallvard

    01/29/2019, 11:49 AM
    So in a browser I receive some JSON in a string through a websocket, and I want to pass it into the constructor that I posted here yesterday. Actually, I overload the constructor (the code is in an mpp and this works fine on the jvm), so it can receive either a string or a map (but not nothing), but I can't seem to get in touch with those constructors. The object I want to construct is a
    Message
    , and the correct way to go about it in js is
    new Message.constructor()
    , right? But I cannot make it use my overloaded constructors, they never seem to get called. Must I name them with annotations in order to use them? (Like
    @JsName("MessageFromString")
    and
    @JsName("MessageFromMap")
    or something?)
    d
    • 2
    • 7
  • s

    spierce7

    01/30/2019, 3:59 PM
    Have there been any comparisons for how large a kotlin-js application is after DCE compared to the equivalent Typescript?
    ➕ 4
    t
    b
    g
    • 4
    • 12
  • s

    sdeleuze

    02/07/2019, 10:11 AM
    Hey, any news on publishing the Kotlin Javascript plugin to Gradle Plugins Portal? Do you plan to do it at some point or do you think this plugin is not something you want to commit supporting on the long term? https://youtrack.jetbrains.com/issue/KT-20156
    a
    h
    • 3
    • 3
  • s

    sdeleuze

    02/07/2019, 10:27 AM
    And on the principle, why is there a need for such
    ((MessageEvent) -> dynamic)?
    signature, why not just
    MessageEvent -> Unit
    ?
    a
    • 2
    • 1
  • s

    sdeleuze

    02/07/2019, 10:40 AM
    I have added a comment here in case you prefer to answer in the bugtracker https://youtrack.jetbrains.com/issue/KT-22635#focus=streamItem-27-3283501-0-0
    a
    • 2
    • 5
  • t

    thana

    02/08/2019, 6:09 PM
    Using jQuery I'm trying to set an eventhandler intercepting the submit event of a form. But when it's called an error is thrown: "apply is not a function". That's true, the EventHandler only has a invoke method. Anyone had luck with this?
    j
    • 2
    • 2
  • m

    michael.barker

    02/09/2019, 6:20 PM
    Is ts2kt still being maintained?
    g
    u
    r
    • 4
    • 4
  • h

    hallvard

    02/11/2019, 11:19 AM
    In an mpp, I am serializing to and from JSON, but using a map internally. I have been having trouble on the deserialization end in JS, but solved most of it with a custom objectToMap function that could populate a map from the JSON object in javascript. But. Lists go into JSON with an array-like notation and are re-read as arrays. From there, I need to get them back into kotlin lists. Could anyone help me get it right? Here's my last attempt (which doesn't work, since toList() isn't a function in js:
    d
    • 2
    • 6
  • s

    SackCastellon

    02/11/2019, 12:41 PM
    Hi, I'm using the Koltin Frontend plugin in a multi-platform project and when running the
    webpack-bundle
    task I get the following error:
    > Task :webpack-bundle
    Hash: df342eca11f1ca4960d1
    Version: webpack 4.29.3
    Time: 1438ms
    Built at: 02/11/2019 1:36:14 PM
             Asset      Size  Chunks             Chunk Names
    main.bundle.js  4.06 MiB    main  [emitted]  main
    Entrypoint main = main.bundle.js
    [../../../../node_modules/webpack/buildin/global.js] (webpack)/buildin/global.js 472 bytes {main} [built]
    [../../../../node_modules_imported/kotlin-extensions/kotlin-extensions.js] D:/Juanjo/Code/UJI/mpp/build/node_modules_imported/kotlin-extensions/kotlin-extensions.js 4.09 KiB {main} [built]
    [../../../../node_modules_imported/kotlin-react-dom/kotlin-react-dom.js] D:/Juanjo/Code/UJI/mpp/build/node_modules_imported/kotlin-react-dom/kotlin-react-dom.js 112 KiB {main} [built]
    [../../../../node_modules_imported/kotlin-react/kotlin-react.js] D:/Juanjo/Code/UJI/mpp/build/node_modules_imported/kotlin-react/kotlin-react.js 18.4 KiB {main} [built]
    [../../../../node_modules_imported/kotlin/kotlin.js] D:/Juanjo/Code/UJI/mpp/build/node_modules_imported/kotlin/kotlin.js 1.7 MiB {main} [built]
    [../../../../node_modules_imported/kotlinx-coroutines-core/kotlinx-coroutines-core.js] D:/Juanjo/Code/UJI/mpp/build/node_modules_imported/kotlinx-coroutines-core/kotlinx-coroutines-core.js 708 KiB {main} [built]
    [../../../../node_modules_imported/kotlinx-html-js/kotlinx-html-js.js] D:/Juanjo/Code/UJI/mpp/build/node_modules_imported/kotlinx-html-js/kotlinx-html-js.js 587 KiB {main} [built]
    [./mpp.js] 5.06 KiB {main} [built]
        + 12 hidden modules
    
    ERROR in D:/Juanjo/Code/UJI/mpp/build/node_modules_imported/kotlin-extensions/kotlin-extensions.js
    Module not found: Error: Can't resolve 'core-js/library/fn/object/assign' in 'D:\Juanjo\Code\UJI\mpp\build\node_modules_imported\kotlin-extensions'
     @ D:/Juanjo/Code/UJI/mpp/build/node_modules_imported/kotlin-extensions/kotlin-extensions.js 116:37-80
     @ D:/Juanjo/Code/UJI/mpp/build/node_modules_imported/kotlin-react/kotlin-react.js
     @ ./mpp.js
    
    
    > Task :webpack-bundle FAILED
    
    FAILURE: Build failed with an exception.
    
    * What went wrong:
    Execution failed for task ':webpack-bundle'.
    > node webpack.js failed (exit code = 2)
    
    * Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
    
    * Get more help at <https://help.gradle.org>
    
    Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
    Use '--warning-mode all' to show the individual deprecation warnings.
    See <https://docs.gradle.org/4.10.3/userguide/command_line_interface.html#sec:command_line_warnings>
    
    BUILD FAILED in 4s
    8 actionable tasks: 3 executed, 5 up-to-date
    Cause: node webpack.js failed (exit code = 2)
    13:36:14: Task execution finished 'webpack-bundle'.
    The project is just a copy of https://github.com/Kotlin/kotlin-frontend-plugin/tree/master/examples/new-mpp, with the timer example from https://reactjs.org/#a-stateful-component to test how KotlinJS works with react.
    • 1
    • 2
  • t

    thana

    02/11/2019, 6:00 PM
    i really learn to hate name mangling
    s
    • 2
    • 1
  • t

    thana

    02/11/2019, 6:13 PM
    oh the problem was
    lateinit
    obviously lateinit and jsname doesnt work together
    s
    • 2
    • 4
  • y

    yousuf.haque

    02/11/2019, 6:35 PM
    if I have a kotlin2js frontend project, and a kotlin2js backend projects, both in gradle in the same repo, is there a way to reuse a data class in both projects from a shared gradle module?
    - root
        - module1
            - build.gradle
        - module2
            - build.gradle
        - sharedmodule
            - src
                - SomeDataClass.kt
            - build.gradle
        - build.gradle
    In the above example, I'd like to use
    SomeDataClass
    in module 1 and module 2 without having to set up a kotlin multiplatform project cc @Zachary Smith
    n
    z
    d
    • 4
    • 5
  • g

    GarouDan

    02/11/2019, 10:09 PM
    Hi guys, someone can point to me an up-to-date repository implementing react with kotlin? With a mutiplatform approach would be nice too 😃
    z
    • 2
    • 2
  • t

    thana

    02/12/2019, 1:28 PM
    i have a value object with a
    Long
    property. when i use
    json.stringify()
    on javascript it creates an object with two properties
    lwo_
    and
    high_
    what, otoho, kotlin isnt able to decoe again... is there any way out of this situation?
    s
    • 2
    • 3
  • j

    jw

    02/12/2019, 4:51 PM
    yes that was added in Gradle 5.1
    g
    s
    • 3
    • 5
  • s

    SOFe

    02/19/2019, 4:12 AM
    Right now I have to use ts2kt if I want strict types
    t
    s
    • 3
    • 10
  • s

    spand

    02/20/2019, 8:10 AM
    webpack-bundle
    g
    • 2
    • 5
  • g

    galex

    02/20/2019, 5:54 PM
    Do anyone have a good example on how to have main.bundle.js divided into multiple little ones? My app is 5.45MB after minification and it says the optimal size is 244Kb? 😮
    r
    g
    • 3
    • 11
  • g

    galex

    02/21/2019, 5:25 AM
    Does its size matter in terms of single page apps nowadays?
    :yes: 5
    g
    • 2
    • 1
  • l

    louiscad

    02/21/2019, 10:17 PM
    Hi, I'm testing building a multiplatform library. I added the
    js
    target and a dependency to the js stdlib, but when I run the
    build
    gradle task, I get the following error:
    org.gradle.execution.MultipleBuildFailures: Build completed with 1 failures.
    	...
            ...
    Caused by: org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':modules:bitflags:compileKotlinJs'.
    	at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:110)
    	...
            ...
    	... 6 more
    Caused by: java.lang.NoSuchMethodError: org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments.setCommonSources([Ljava/lang/String;)V
    	at org.jetbrains.kotlin.compilerRunner.GradleCompilerRunner.runJsCompilerAsync(GradleKotlinCompilerRunner.kt:297)
    	at org.jetbrains.kotlin.gradle.tasks.Kotlin2JsCompile.callCompilerAsync$kotlin_gradle_plugin(Tasks.kt:566)
    	at org.jetbrains.kotlin.gradle.tasks.Kotlin2JsCompile.callCompilerAsync$kotlin_gradle_plugin(Tasks.kt:476)
    Here's the link to the gradle.kts file of the module I'm trying to build: https://github.com/LouisCAD/Splitties/blob/baaf3548141ac5f8e631b5f858cc9ce25bfa2cbe/modules/bitflags/build.gradle.kts#L28 and here's the
    buildSrc
    it depends on: https://github.com/LouisCAD/Splitties/tree/baaf3548141ac5f8e631b5f858cc9ce25bfa2cbe/buildSrc I'm using Kotlin 1.3.21 with Gradle 4.10.1, and AGP 3.3.1 for the Android part.
    d
    j
    n
    • 4
    • 9
  • a

    Azaryan

    02/22/2019, 9:06 PM
    Need to start kotlin js project to use https://www.cypress.io/ . Where I can read a starter guide or similar ( how to start js project, how to use js libraries ). Thanks in advace
    l
    • 2
    • 2
  • g

    galex

    02/23/2019, 2:57 PM
    Is there a way to convert ArrayBuffer to a ByteArray?
    d
    s
    • 3
    • 17
Powered by Linen
Title
g

galex

02/23/2019, 2:57 PM
Is there a way to convert ArrayBuffer to a ByteArray?
d

Dico

02/23/2019, 3:45 PM
Copy the contents
s

Svyatoslav Kuzmich [JB]

02/23/2019, 3:55 PM
You can create Int8Array around the buffer and then unsafe cast it to ByteArray
You can find other type mappings here: https://kotlinlang.org/docs/reference/js-to-kotlin-interop.html#representing-kotlin-types-in-javascript
d

Dico

02/23/2019, 4:00 PM
Oh that's cool
g

galex

02/23/2019, 5:24 PM
Thank you so much !!!!! It worked ! For days I am trying to get a file to upload correctly to a backend through ktor-client !
@Suppress("CAST_NEVER_SUCCEEDS")
fun ArrayBuffer?.asByteArray(): ByteArray? = this?.run { Int8Array(this) as ByteArray }
s

Svyatoslav Kuzmich [JB]

02/23/2019, 5:41 PM
Great! We highly appreciate feedback. Please let us know if you have any inconveniences.
g

galex

02/23/2019, 5:47 PM
I’d like that function to be part of kotlin/js by default
👍 1
Would have been useful
s

Svyatoslav Kuzmich [JB]

02/23/2019, 5:55 PM
Since we know for sure that
ByteArray
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$;
I’d like that function to be part of kotlin/js by default
Could you file a feature request at kotl.in/issue?
g

galex

02/23/2019, 6:13 PM
Nice I will use that then
Yes I will submit a feature request
I didn’t know about unsafeCast, I am going to apply it everywhere needed now
s

Svyatoslav Kuzmich [JB]

02/23/2019, 6:22 PM
unsafeCast
has downsides, if cast fails your program will fail the JavaScript way: usually farther down the execution with ambiguous error message 😁
g

galex

02/23/2019, 6:23 PM
ahah
https://youtrack.jetbrains.com/issue/KT-30098?project=kt
View count: 23