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

    Ben Edwards

    08/11/2022, 3:48 PM
    Out of interest how does this slack relate to the 'official' one? I actually prefer the channels here. They are spot on 🙂.
    r
    j
    +2
    5 replies · 5 participants
  • b

    Ben Edwards

    08/11/2022, 4:28 PM
    Doing JetBrains Basic Kotlin and it does not like the formatting of the first 3 val statement.
    fun main() {
        val min_sleep: Int = readln().toInt()
        val max_sleep: Int = readln().toInt()
        val hours_slept: Int = readln().toInt()
    
        println(
            if (hours_slept < min_sleep) {
                "Deficiency"
            } else if (hours_slept > max_sleep) {
                "Excess"
            } else {
                "Normal"
            }
        )
    }
    Variable names should match the pattern: [a-z][A-Za-z0-9]* What am I missing?
    k
    r
    +3
    9 replies · 6 participants
  • j

    juh juh

    08/11/2022, 8:03 PM
    Does anybody know how to make "Show kotlin bytecode" in Itnellij IDEA work with kotlin multiplatform? For me, it just displays an error, while working fine for non-kotlin multiplatform projects
    k
    m
    +1
    4 replies · 4 participants
  • p

    poohbar

    08/12/2022, 3:28 AM
    Still learning about generics. In this example I would expect this to work, because I feel like
    Animal<Int>
    fits into
    Animal<Any?>
    . What am I missing?
    open class Animal<T>
    class Dog: Animal<Int>()
    val x : Animal<Any?> = Dog() // error at the constructor call
    j
    j
    +2
    16 replies · 5 participants
  • m

    Muhammad Talha

    08/12/2022, 5:47 AM
    Hi, I had a question about idiomatically returning a value from a callback function in Kotlin. In option A below I don’t need any qualified return and looks less “weird” to me. However, in other langs I usually see advocation of moving conditions “left”. So in B I’ve tried this. But now I need a qualified return in the condition. I have a feeling A is more idiomatic in Kotlin because we don’t need to unnecessarily add the qualified retun but this also goes against my desire to move as much “left” as possible 😅. Is there another way?
    // A)
    
    validate { credential ->
        // ...
        if (/* ... */) {
            Some("thing")
        } else {
            null
        }
    }
    
    // B)
    
    validate { credential ->
        // ...
        if (/* ... */) {
            return@validate Some("thing")
        }
    
        null
    }
    g
    j
    3 replies · 3 participants
  • e

    elye

    08/12/2022, 8:36 AM
    Probably a newbie question? I have 2 enums that cross-reference each other
    enum class School(val zone: String, val captain: Student) {
        Metricon("New York", Student.David),
        Carlisle("London", Student.Paul)
    }
    
    enum class Student(val lastName: String, val school: School) {
        David("Samuel", School.Metricon),
        Solomon("Handsome", School.Metricon),
        Saul("Black", School.Metricon),
        Paul("Lewis", School.Carlisle),
        Joseph("Hardy", School.Carlisle),
        John("Baptise", School.Carlisle),
    }
    When I print it out
    val student = Student.David
            val school = School.Carlisle
    
            println(student.school)
            println(school.captain)
            println(student.lastName)
            println(school.zone)
    It resulted in
    Metricon
    null
    Samuel
    London
    Notice the
    null
    there. The
    school.captain
    is missing and became null. How can I solve this problem?
    r
    m
    +1
    4 replies · 4 participants
  • n

    nbento.neves

    08/12/2022, 9:17 AM
    Hi, I saw a
    kt
    file with an
    object
    inside an
    object
    . I would like to know if someone can help me to understand the difference between using the
    object
    and a
    companion object
    . Is this the right way to use
    object
    ?
    object Type {
        object USER {
            const val KEY  = "key"
            const val NAME  = "name"
        }
    }
    a
    2 replies · 2 participants
  • p

    poohbar

    08/12/2022, 3:17 PM
    Ran into some very strange behavior. Why does it matter when I call
    ::class
    ?
    val c1 = Int::class.java to 42::class.java 
    c1.first to c1.second // (int, int)
    
    val c2 = Int::class.java to 42
    c2.first to c2.second::class.java // (int, class java.lang.Integer)
    j
    e
    7 replies · 3 participants
  • i

    Ishan Iqbal

    08/12/2022, 4:44 PM
    👋 Hello, team!
  • i

    Ishan Iqbal

    08/12/2022, 4:44 PM
    i need a mentor to help mei
  • i

    Ishan Iqbal

    08/12/2022, 4:45 PM
    i am right now completing the courses and codelabs present in android courses
  • l

    Landry Norris

    08/12/2022, 4:49 PM
    When you have code at the top level of a file named FileName.kt, Kotlin creates a class named FileNameKt by default. This is where MainKt comes from. However, you have Main.kt in a package named cinema, so you should use cinema.MainKt
    k
    j
    +1
    15 replies · 4 participants
  • b

    Ben Edwards

    08/12/2022, 7:44 PM
    TLDR: The JetBrains Academy Basic Kotlin course is causing me to lose the will to live (I've done lots of courses and non have had this effect on me). Can someone please recommend an alternative one for Basic Kotlin? The below seems like a bit of a stretch. "In other words, the
    =
    sign does not copy the object itself, it only copies a reference to it.". This only seems the case for immutable objects. if I made the variable a var this is not the case. This seems very important. I'm getting very disillusioned with the JetBrains Academy course. It's very slow going but still manages to seem to oversimplify things. Can anyone recommend a good beginners course with lots of excursuses? So far my favourite one is https://www.udemy.com/course/100-days-of-code/ (bit its a Python course). It covers a lot and the docent is not too bogged down with the fiddly, not very useful stuff. I really appreciate the effort they put in but I want something that is a bit more fun. I don't mind paying a bit.
    l
    e
    +3
    38 replies · 6 participants
  • a

    Abdullah Samir

    08/13/2022, 12:40 AM
    Hello, I am sorry to ask these simple questions. I need to know that , the jvm that kotlin run its code in it belongs to any company??? And if the company that owns jvm blocked the kotlin to use it what will happen here?? Thank you
    c
    j
    +2
    8 replies · 5 participants
  • b

    Ben Edwards

    08/13/2022, 1:40 PM
    Curios, for constants you use 'const val', why not just const?
    c
    s
    +1
    12 replies · 4 participants
  • o

    oday

    08/14/2022, 6:55 PM
    is there a shorter way of writing this?
    val names = ArrayListQueue<String>()
    
        map["you"]?.forEach{
            names.enqueue(it)
        }
    l
    y
    +2
    9 replies · 5 participants
  • j

    juh juh

    08/14/2022, 9:04 PM
    Is there a way to subdivide kotlin multiplatform project into modules? Aka, so code from one module can't use code from other module unless its module depends on that other module. I know that I can do localmaven and publishing, but I still want to keep it all inside one project so I can edit code from one IntelliJ IDEA instance, and all the maven stuff seems like an overkill
    y
    2 replies · 2 participants
  • r

    Rob Elliot

    08/15/2022, 8:58 AM
    If you implement a Java interface that defines a getter:
    interface Something {
      String getAConstant();
    }
    and you want the value of
    getAConstant
    to be a constant, is there a less verbose way than this?
    class ConcreteSomething : Something {
      private val _constant = "constant value"
      override fun getAConstant(): String = _constant
    }
    (It's not actually a String, it's calculated by calling other methods on a supertype using a constructor value, just simplifying things.)
    k
    j
    +1
    6 replies · 4 participants
  • h

    Hassaan

    08/15/2022, 9:39 AM
    In kotlin how to create a String say “hello” twice in heap memory? In java, we do this with the help of ‘new’ keyword : new String(“hello”)
    j
    s
    5 replies · 3 participants
  • s

    svenjacobs

    08/15/2022, 12:59 PM
    Hello, I would like to implement the class delegation pattern by delegating to a property which is initialized during class initialization. I think technically this should work but Kotlin 1.7.10 doesn’t accept this construct. The reason why I want to delegate to a property is because when I create an instance of the delegate, I need to access an extension value which is declared in a third-party library. Is there an alternative besides having to implement the delegation manually? Is this something that a future version of Kotlin might support?
    class Derived(
        factory: (dep: Dependency) -> Base,
    ) : SomeBaseClass(), Base by delegate {
    
        private val delegate = factory(dependency)
    }
    
    val SomeBaseClass.dependency: Dependency
      get() = ...
    m
    5 replies · 2 participants
  • j

    juh juh

    08/15/2022, 2:22 PM
    If I have a class in
    commonMain
    that I want to provide specific implementation on ONLY ONE multiplatform target, while using default common implementation for other, is there a way to do it without making class
    expect
    and spamming
    actual
    in every target?
    j
    s
    4 replies · 3 participants
  • j

    Jasmin Fajkic

    08/15/2022, 4:29 PM
    Hello. I am coming from JS world and want to ask two things: 1. how to replace / from start string? i have path /path/something and want to remove first slash. Tried with
    path.replace("/^\\/|\\/\$/g".toRegex(), "")
    does not work. 2. what is easiest way to get search params as key value pair. /path/something?boom=true&id=bam. Want to have search params like boom:true , id: bam?
    l
    e
    +1
    6 replies · 4 participants
  • s

    sreich

    08/15/2022, 5:49 PM
    it appears to me that kotlin is more sensitive at the class level, at order of operations? it seems to process class files/fields vertically. I don't recall java having as many cases as this does. But for instance, a class level property being referenced by the init {} block, the property must be above. is this behavior actually different, or just something i haven't run into enough before? What is it called?
    e
    v
    8 replies · 3 participants
  • p

    poohbar

    08/15/2022, 7:36 PM
    Does Kotlin have an equivalent of the new Java feature:
    if (obj instanceOf String myString) {
      // myString
    }
    I know I can do
    as
    and get a smart cast but I can’t give it a new name in Kotlin, right?
    v
    j
    +5
    25 replies · 8 participants
  • s

    sreich

    08/15/2022, 8:03 PM
    class CliOption2<T>(additionalProperties: MutableMap<String, Any>) {
            val value by lazy {
                val t = additionalProperties[cliOption.opt]!! as? T
                t ?: throw NotImplementedError("cli option could not be casted")
                t
    } how can i fix the cast warning here? I thought me throwing something if it fails would work. Why is it still considered unsafe and how can i fix this?
    e
    r
    19 replies · 3 participants
  • j

    Jerry Yion

    08/16/2022, 2:44 AM
    Hi all, I am wondering what testing framework are you using in the project now, like mockk/mockito/kotest~~~
    a
    g
    +4
    21 replies · 7 participants
  • n

    Nilotpal Saha

    08/16/2022, 7:50 AM
    Hey team is there any channel for KMM platform.
    a
    1 reply · 2 participants
  • s

    Shervin

    08/16/2022, 9:53 AM
    Hey everyone! Silly question: is there a channel for 'Kotlin' itself ?! 😄 I'm looking for docs/conventions/ideas about when to use
    if
    and when to use
    when
    as the line between these two are very thin and we want to have an agreement in our team about their usage.
    s
    5 replies · 2 participants
  • k

    Kenneth

    08/16/2022, 10:45 AM
    Is there a find or throw method on lists?
    s
    o
    2 replies · 3 participants
  • m

    M Saif Ullah

    08/16/2022, 11:52 AM
    hi
Powered by Linen
Title
m

M Saif Ullah

08/16/2022, 11:52 AM
hi
View count: 2