https://kotlinlang.org logo
Docs
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
announcements
  • l

    LeoColman

    06/25/2019, 9:07 PM
    For a project with 80~90k LOC it takes up to 15s for a file to compile
    d
    • 2
    • 1
  • r

    Robert Menke

    06/25/2019, 9:08 PM
    Hmm what kind of specs does your computer have?
    l
    • 2
    • 2
  • r

    Robert

    06/25/2019, 10:06 PM
    Can anyone explain to me what this IDE errors means, and even better, maybe how to solve it? What is wrong?
    IntelliJ_IDEA_error_debugging_kotlin_project.txt
    ❓ 1
    a
    g
    • 3
    • 3
  • v

    victoralissont5

    06/25/2019, 11:10 PM
    Moreover, Kotlin's concept of suspending function provides a safer and less error-prone abstraction for asynchronous operations than futures and promises. Could someone explain why this is true?
    s
    g
    • 3
    • 4
  • a

    aidan

    06/26/2019, 8:34 AM
    Is there a profiler for kotlin?
    g
    n
    • 3
    • 2
  • r

    rdgoite

    06/26/2019, 3:47 PM
    Hello everyone. I’m trying to run my Kotlin Scratch files on Intellij, but it doesn’t give me the option to run it. I came across this issue that’s 4 years old, and seems to have been labeled done. Is this still a known issue? https://youtrack.jetbrains.com/issue/KT-10368
    n
    • 2
    • 1
  • n

    nikolaymetchev

    06/26/2019, 6:15 PM
    Hello Everyone, Does anyone know if there is a Kotlin DSL that can be used to generate Java code (or even better generate Kotlin Code).
    r
    s
    • 3
    • 3
  • h

    Hullaballoonatic

    06/27/2019, 2:21 AM
    I've started getting addicted to this format instead of abstract or open classes, and I imagine that's a bad thing:
    interface Foo {
        val foo: Int
        val bar: String?
    
        companion object {
            operator fun invoke(foo: Int, bar: String? = null) = object : Foo {
                override val foo = foo
                override val bar = bar ?: otherLogic...
                // etc
            }
        }
    }
    In a lot of ways it's like having the ability to inherit from multiple classes. That's probably not a good freedom to have, though.
    invoke
    operator has a few advantages over
    constructor
    , too, such as error checking the arguments prior to instantiating the object. This also opens up delegation to cut down on code You'd think it'd be unfortunate you couldn't do this:
    class Foozle(foo: Int, bar: String?) : Foo(foo, bar)
    But you can instead just do this:
    class Foozle(foo: Int, bar: String?) : Foo by Foo(foo, bar)
    I'd love to hear the drawbacks to this overall approach
    😱 1
    a
    r
    m
    • 4
    • 8
  • p

    Paul Woitaschek

    06/27/2019, 8:17 AM
    For extension functions. When do you use the prefix
    as
    and when do you use the prefix
    to
    ?
    r
    m
    +2
    • 5
    • 12
  • b

    bod

    06/27/2019, 8:24 AM
    Hello, World! I have a strange issue, wondering if it's already known. I have a method with this signature:
    fun foo(vararg pairs: Pair<String, (String) -> FooBar>)
    When I call it like this:
    foo(
               "a" to { _ -> FooBar() },
               "b" to { _ -> FooBar() }
          )
    I get warning saying that
    _ ->
    is redundant. But, if I remove it I get
    Type inference failed. Expected type mismatch: inferred type is Pair<String, () -> FooBar> but Pair<String, (String) -> FooBar> was expected
    Could this be a problem with the "new type inference algorithm"?
    k
    r
    • 3
    • 6
  • n

    Neil

    06/27/2019, 9:16 AM
    How does a parent class execute code at the end of construction, after all the subclass' properties have been initialized and their init{} blocks run?
    p
    k
    +2
    • 5
    • 27
  • m

    mike_shysh

    06/27/2019, 10:52 AM
    Hi, if an argument in some function is
    Class<T> type
    Is it possible to provide List<ObjectA>.class (seems such syntax is not ok in kotlin)?
    d
    d
    • 3
    • 10
  • j

    juliocbcotta

    06/27/2019, 1:55 PM
    Hi, Would be possible to have a version of this method using generics? So the interface could be
    T
    fun getListener(): MyListenerInterface {
    return when {
                parentFragment is MyListenerInterface -> parentFragment as MyListenerInterface
                targetFragment is MyListenerInterface -> targetFragment as MyListenerInterface
                else -> context as MyListenerInterface
            }
    }
    k
    • 2
    • 4
  • j

    jw

    06/27/2019, 2:07 PM
    1.3.40 shipped a
    typeOf
    intrinsic to create a
    KType
    but you cannot get a
    Type
    from that without kotlin-reflect... yet https://youtrack.jetbrains.com/issue/KT-32241
    👍 2
    n
    • 2
    • 1
  • l

    LS

    06/27/2019, 3:46 PM
    is there a thread-safe version of
    mutableMapOf()
    ? i need the map to be thread-safe, and also hold its entries in the order of insertion.
    k
    b
    • 3
    • 8
  • n

    Nicole

    06/27/2019, 5:03 PM
    Someone did several comparisons with
    Float.NaN
    which led to this odd result:
    Float.NaN === Float.NaN
    is
    false
    with JVM and JS targets, and
    true
    with Kotlin/Native. — I see that identity equality for
    Float
    has been deprecated, but still figured I should check if this is a bug (and should be reported) or if it’s just “undefined behavior”
    k
    d
    • 3
    • 20
  • h

    Hullaballoonatic

    06/27/2019, 6:42 PM
    Is kotlin now using
    &
    ,
    |
    for anything? Bitwise operators? Would be kinda neat if
    |
    could be a new bracket type if kot, but that would be weirdly different from industry standard.
    k
    j
    • 3
    • 6
  • s

    streetsofboston

    06/27/2019, 7:32 PM
    In the kotlin playground (play.kotlinlang.org), is there a way to print out an uncaught exception from a Coroutine (when calling
    launch
    from a scope without a CoroutineExceptionHandler)? Update: Solved
    k
    • 2
    • 8
  • m

    mathew murphy

    06/27/2019, 10:22 PM
    launch (newSingleThreadContext("DedicatedThread")) {
        Thread.sleep(1000L)
    }
    g
    g
    • 3
    • 2
  • l

    laimiux

    06/27/2019, 10:22 PM
    Weird question: are there any reason why we can’t do (besides current compiler)
    val optional: String? = if (someCondition) { "" }
    j
    g
    r
    • 4
    • 4
  • v

    ValV

    06/28/2019, 12:48 PM
    How to use
    kotlinx.cli
    in project (Gradle)? I'm not sure that I'm adding it correctly as a dependency (it is not found). Or I'm missing something else?
    g
    • 2
    • 6
  • h

    haroldadmin

    06/28/2019, 4:40 PM
    What coroutine scope do you use to launch coroutines in classes that don’t necessarily have a lifecycle? I’m creating a NotificationManager class in an Android application, and I need to use coroutines for taking some database related tasks off the main thread. This class will be used as a singleton in the project via dependency injection. • Should I implement the Coroutine Scope interface in it? There will be no lifecycle callback to cancel the running coroutines in this scope, though • Should I just use GlobalScope if I don’t have a way to cleanup resources anyway?
    r
    g
    • 3
    • 4
  • m

    mathew murphy

    06/28/2019, 5:21 PM
    Inside a
    with (foo)
    block, is there any way to indicate that I mean variable
    bar
    from outside the block, rather than a property
    bar
    of object
    foo
    ?
    b
    l
    g
    • 4
    • 6
  • m

    mathew murphy

    06/28/2019, 5:57 PM
    Is there something that's like a combo of
    use
    and
    with
    ?
    s
    b
    +2
    • 5
    • 19
  • t

    Timothy Wong

    06/28/2019, 7:08 PM
    Hi guys, is this some compiler bug? With the code https://hasteb.in/tosejohu.kotlin, the node.left and node.right are not automatically casted as thecorrect types, even though the if statement checks, and we can see from the docs that it technicaly should. https://kotlinlang.org/docs/reference/typecasts.html
    var local variables - if the variable is not modified between the check and the usage, is not captured in a lambda that modifies it, and is not a local delegated property;
    k
    s
    s
    • 4
    • 12
  • l

    Luis Munoz

    06/28/2019, 7:40 PM
    If I have a kotlin class that takes a function as an argument (a block). How do I initialize that class from java, link to documentation might suffice. I don't see that in https://kotlinlang.org/docs/reference/java-interop.html ?
    s
    • 2
    • 11
  • k

    kevinherron

    06/28/2019, 7:41 PM
    anyone know if it’s expected that some String.format calls don’t work with unsigned types?
    b
    g
    • 3
    • 5
  • n

    Nikolai

    06/29/2019, 5:31 AM
    Hi, everyone I have simple question, maybe it's already answered somewhere but didn't managed to find answer. In Kotlin documentation about lambda formatting :
    If a call takes a single lambda, it should be passed outside of parentheses whenever possible.
    and IDEA always show warning in code about this. I wondering why it should be formatted in that way? Is it just style and readability guidelines? Or it have technical meaning behind it?
    g
    • 2
    • 2
  • f

    Fudge

    06/29/2019, 6:30 AM
    Is there a way to add a method to an enum class without reflection? If I have
    enum class Test{
    foo
    }
    Then kotlin comes built in with
    values(), valueOf()
    , but how can I add a new helper method like
    getValueNames()
    ?
    g
    • 2
    • 5
  • g

    gianluz

    06/29/2019, 3:48 PM
    Hello everyone!! i have a problem executing a script with dependencies: here my script file:
    @file:Repository("<https://repo1.maven.org/maven2>")
    @file:DependsOn("org.apache.commons:commons-text:1.6")
    
    import org.jetbrains.kotlin.script.util.DependsOn
    import org.jetbrains.kotlin.script.util.Repository
    
    import org.apache.commons.text.WordUtils
    
    val hello = "helloWorld"
    print(hello)
    print(WordUtils.capitalize(hello))
    then i execute the script:
    kotlinc -cp /usr/local/lib/danger/kotlin-main-kts.jar -script script.main.kts
    script.main.kts:7:12: error: unresolved reference: apache
    import org.apache.commons.text.WordUtils
               ^
    script.main.kts:11:7: error: unresolved reference: WordUtils
    print(WordUtils.capitalize(hello))
          ^
    basically doesn't resolve the dependencies 😕 and i don't want to link directly the jar when executing the script
    s
    • 2
    • 1
Powered by Linen
Title
g

gianluz

06/29/2019, 3:48 PM
Hello everyone!! i have a problem executing a script with dependencies: here my script file:
@file:Repository("<https://repo1.maven.org/maven2>")
@file:DependsOn("org.apache.commons:commons-text:1.6")

import org.jetbrains.kotlin.script.util.DependsOn
import org.jetbrains.kotlin.script.util.Repository

import org.apache.commons.text.WordUtils

val hello = "helloWorld"
print(hello)
print(WordUtils.capitalize(hello))
then i execute the script:
kotlinc -cp /usr/local/lib/danger/kotlin-main-kts.jar -script script.main.kts
script.main.kts:7:12: error: unresolved reference: apache
import org.apache.commons.text.WordUtils
           ^
script.main.kts:11:7: error: unresolved reference: WordUtils
print(WordUtils.capitalize(hello))
      ^
basically doesn't resolve the dependencies 😕 and i don't want to link directly the jar when executing the script
s

Shawn

06/29/2019, 11:44 PM
#scripting might be of more help
View count: 5