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
  • b

    Bruno Medeiros

    02/26/2021, 12:51 AM
    A new version 203-1.4.31-release-IJ7148.5 of the Kotlin plugin is available.
    Couldn't find any release notes on Kotlin 1.4.31 (searched on Google and checked here), am I missing somehing?
    z
    v
    • 3
    • 5
  • p

    Paul Woitaschek

    02/26/2021, 6:37 AM
    Has someone ever faced this?
    a
    a
    +5
    • 8
    • 10
  • d

    Dominick

    02/26/2021, 7:20 AM
    Would someone be able to explain this code line by line (for the first two lines, the findAll line and the return lines) for me?
    private const val WORD_PATTERN = """[A-Za-z][A-Za-z']*"""
    private val WORD_REGEX = Regex(WORD_PATTERN)
    
    fun top3(s: String): List<String> {
        val words = WORD_REGEX.findAll(s).map{ it.groupValues[0].toLowerCase() }
        val occurrences = mutableMapOf<String, Int>()
        for (word in words) {
            occurrences[word] = (occurrences[word] ?: 0) + 1
        }
        return occurrences.toList()
            .sortedByDescending{ it.second }
            .map{ it.first }
            .take(3)
    }
    :nope: 1
    z
    n
    • 3
    • 4
  • a

    Alexander Karkossa

    02/26/2021, 11:59 AM
    Because of this: https://jfrog.com/blog/into-the-sunset-bintray-jcenter-gocenter-and-chartcenter/ any ideas, when kotlinx-collections-immutable-jvm and otheres are available on mavenCentral?
    • 1
    • 1
  • r

    Rob Elliot

    02/26/2021, 2:52 PM
    Is there a neat trick to have both reified and inferred type parameters on the same function? It seems the moment I have to provide one I have to provide all, which is making me sad.
    r
    z
    y
    • 4
    • 6
  • p

    peekandpoke

    02/26/2021, 2:53 PM
    Hi! Is anyone else also experiencing problems in the IntelliJ with sealed classes and the
    when() { is .. }`
    I have a when and i am covering all sub-classes of a sealed class, still the IDE shows me an error. The compiler is fine with the code? I have the newest plugin 1.4.31 and the kotlin version of the project is 1.4.31 as well. Gradle 6.8.1 ... any thoughts?
    ➕ 2
    m
    • 2
    • 2
  • c

    CLOVIS

    02/26/2021, 7:10 PM
    If I understand correctly: • Kotlin/JVM is best for “I want to be as fast as possible (at the cost of RAM and startup time)” • Kotlin+GraalVM is best for “I don't want to wait for the VM to start (at the cost of throughput)” • Kotlin/Native is best for “I want to use as little RAM as possible (at the cost of speed)” Is that correct?
    ✅ 1
    u
    n
    +4
    • 7
    • 45
  • b

    bsimmons

    02/26/2021, 8:14 PM
    Apparently I'm a Kotlin nab. How do I get something like this working? I'm looking for a way to combine two interfaces that are from external libs.
    j
    p
    +2
    • 5
    • 15
  • k

    Karlo Lozovina

    02/27/2021, 12:08 AM
    How to tell kotlinx-benchmark to run only single benchark, not everything?
    i
    • 2
    • 2
  • u

    user

    02/27/2021, 5:20 PM
    message has been deleted
    🥴 1
    d
    • 1
    • 1
  • j

    jacksparling

    02/28/2021, 12:47 PM
    help please. I can't use android studio canary 8 arctic fox in macbook big sur
    :google: 1
    l
    i
    +2
    • 5
    • 10
  • s

    Shivam Sethi

    02/28/2021, 7:20 PM
    General question, when will android studio, java will be optimized for apple silicon? Currently it is usable but not compared to native applications like xcode
    k
    • 2
    • 1
  • t

    therealbluepandabear

    03/01/2021, 12:10 AM
    I have this weird fear of failing.
    c
    • 2
    • 2
  • t

    therealbluepandabear

    03/01/2021, 3:57 AM
    I like how Kotlin removes the need for semicolons. There is absolutely little-to-no point in having semicolons in coding languages these days. When I used to code in C# (and later in Java) I never understood the point of having to press the semicolon with my pinkie finger after each statement. Who else agrees with this?
    s
    s
    +2
    • 5
    • 7
  • a

    Abhishek K

    03/01/2021, 12:51 PM
    Hey folk's, I'm new to Kotlin. Previous I have developed Apps using Java. Not its time to start Kotlin. Any suggestions
    c
    • 2
    • 7
  • c

    Chills

    03/01/2021, 10:30 PM
    Hi! there is a definite need for my startup to do Mobile based app What criteria should i consider apart from performance,build fast and nice gui to choose one language over other like kotlin over traditional platform (android/ios)
    k
    • 2
    • 1
  • t

    therealbluepandabear

    03/02/2021, 5:25 AM
    Also I have this question which I hope you could answer: when should I use nullable types in Kotlin? Should you always use nullable types or just when they are needed? I am a bit confused. Thank you.
    y
    k
    +3
    • 6
    • 5
  • v

    viralshah

    03/02/2021, 7:06 AM
    Hi guys, I am looking for an elegant way of accomplishing the following: I have a sorted list of objects ranging from 0 to 43000 (number of items in list) I would like to get a list of lists where each list contains 5% of the list. [[top 5% of items], [next 5% of items]…] We should have 20 smaller lists now, I wanted to just have empty lists at the end if we run out of items. So far I have tried the chunked function
    val myList = (1..100).toList().chunked(5) -> gives me the right answer
    
    val result = (1..45).toList().chunked(5)) -> gives me only 9 chunks
    Is it possible to add empty lists for the remaining 11 chunks?
    y
    p
    • 3
    • 2
  • c

    Conor Fennell

    03/02/2021, 12:43 PM
    Hey, I was messing around with covariance and contravariance and did not expect this to work. It looks like I created a function which is bivariant. How is that possible as the following code compiles and runs.
    class Container<in T>(private val t: T) 
    
    open class Noun
    open class City: Noun()
    class Capital: City()
    
    fun goCity(container: Container<City>) {
        println(container)
    }
    
    fun main() {
        goCity(Container(Noun())) // pass in a super type of City, this is what I would expect as it is defined contravariance
        goCity(Container(Capital())) // pass in a sub type of City, would not have expected this as it is defined contravariance
    }
    m
    • 2
    • 2
  • l

    Luciano

    03/02/2021, 12:47 PM
    hello, I'm fairly new to Kotlin and I'm working on a Kotlin project that uses Gradle. When running a simple Kotlin Unit test, Intellij Idea runs the gradle
    test
    task rather then executing the test directly (like in a Java project). Is there a way to run the test directly in Intellij?
    r
    r
    • 3
    • 6
  • l

    Luciano

    03/02/2021, 5:48 PM
    Following up on a previous question (https://kotlinlang.slack.com/archives/C0922A726/p1614689220294100) , every time I execute a Kotlin test in Intellij Idea, with or without code modifications, the IDE seems to rebuild the project the test lives in. The rebuild process takes over 30 seconds, even if the project is quite small (2.6GHZ MacBook pro, 32Gb ram). Any idea how to speed up the process?
    c
    c
    • 3
    • 7
  • t

    Tianyu Zhan

    03/02/2021, 6:54 PM
    Hello! I am fairly new to kotlin. Does anyone know how should I write the query when the response is the List in this case? i would appreciate if someone can help with that!
    e
    • 2
    • 2
  • t

    therealbluepandabear

    03/03/2021, 3:20 AM
    What is a lambda expression? I am confused what this is:
    b
    g
    a
    • 4
    • 36
  • t

    therealbluepandabear

    03/04/2021, 12:24 AM
    I'm not understanding inline classes... What do they do? I only have a small amount of knowledge of the JVM and stack/heap stuff. etc. I am trying to improve my knowledge in that sector of things.
    m
    s
    r
    • 4
    • 3
  • t

    therealbluepandabear

    03/04/2021, 12:27 AM
    We could just use a data class with one property
    n
    • 2
    • 6
  • r

    Robert MacLean

    03/04/2021, 8:47 AM
    Hi People, I am struggling with a client who wants only people with Kotlin/JVM experience cause that is what they use (it makes sense for the ask) but my struggle is that Kotlin people are hard to find and I’ve stated that getting other language developers and skilling up is viable. I believe the skilling up from other languages like C# or JS to Kotlin is not difficult and there is overlap of skills (especially for C#). Would love to know if you agree or disagree with me and what your experience coming from other languages to Kotlin?
    g
    t
    +4
    • 7
    • 14
  • x

    xii

    03/04/2021, 12:54 PM
    is it possible to create a sub enum class from an enum class?
    r
    • 2
    • 10
  • s

    SirNapkin1334

    03/04/2021, 5:44 PM
    since StringBuilder is an
    expect class
    , how would one go about adding functionality to it? say I was a kotlin dev and wanted to add a new function, where would the actual implementation be? or if i wanted to modify the signature of an existing function by adding a new argument with a default value
    a
    • 2
    • 5
  • s

    Slackbot

    03/04/2021, 5:45 PM
    This message was deleted.
    :thread-please: 2
    v
    s
    • 3
    • 4
  • c

    Cdev

    03/04/2021, 7:55 PM
    Kotlin Multiplatform Mobile question... I have the following:
    inline fun <reified T : Any> jsonToConfig(stringJson: String): T? =
        Json.decodeFromString<T?>(stringJson)
    Using Kotlinx Serialization decoding from the string works fine in Kotlin. But in Swift: let projectConfig: ProjectConfig? = config.jsonToConfig<ProjectConfig>(stringJson: jsonString) I get the following error: "Cannot explicitly specialize a generic function". Any ideas how I could go about getting this to work?
    a
    l
    • 3
    • 4
Powered by Linen
Title
c

Cdev

03/04/2021, 7:55 PM
Kotlin Multiplatform Mobile question... I have the following:
inline fun <reified T : Any> jsonToConfig(stringJson: String): T? =
    Json.decodeFromString<T?>(stringJson)
Using Kotlinx Serialization decoding from the string works fine in Kotlin. But in Swift: let projectConfig: ProjectConfig? = config.jsonToConfig<ProjectConfig>(stringJson: jsonString) I get the following error: "Cannot explicitly specialize a generic function". Any ideas how I could go about getting this to work?
a

Anders

03/04/2021, 8:45 PM
Swift doesn't support the Kotlin call site specialization syntax e.g.
obj.foo<Type1, Type2>()
. But most importantly, there is no concept of "generic functions" in Objective-C, only generic classes. So if you inspect the function signature which Swift imports from Objective-C, you will see it isn't a generic function at all. like
class FileNameKt {
    class func jsonToConfig(string: String): Any?
}
and I am not sure what will happen when you call it. Undefined behaviour likely. You will either need to define a non-generic version that calls the generic version, or wrapping the function into a generic class if you do need to have it available in its generic form in ObjC/Swift.
fun jsonToMyConfig(string: String): MyConfig? = jsonToConfig<MyConfig>(string)
or
class Converter<T: Any> {
  fun convert(string: String): T? = jsonToConfig<T>(string)
}
c

Cdev

03/04/2021, 9:09 PM
Thanks for info. I'm trying to get the generic form working but when I tried using your example I get "Cannot use T as reified type parameter. Use a class instead".
l

louiscad

03/07/2021, 1:13 PM
@Cdev What you are trying to do can only be done in Kotlin code. Wrap it so before calling from Swift.
✅ 1
c

Cdev

03/09/2021, 2:25 PM
Makes sense, thanks.
View count: 8