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

    Ahmed Mourad

    09/09/2020, 11:32 PM
    Just published an article about value-based classes and using Kotlin's type system to enforce your domain rules. https://medium.com/@dev.ahmedmourad73744/value-based-classes-and-error-handling-in-kotlin-3f14727c0565?source=friends_link&sk=a16186408e1c8e317e3e11fd16e33710
    👍 2
    n
    a
    a
    • 4
    • 7
  • s

    Scott Christopher

    09/10/2020, 1:43 AM
    I'm looking at a way to be able to produce an anonymous object that implements multiple interfaces, but seemingly missing something with the below resulting in a
    Type mismatch. Required: P, Found: <P>
    error for the returned value.
    interface A
    interface B
    interface C
    
    fun <P> resolve(): P where P : A, P : B, P : C {
        return object : A, B, C {}
    }
    Is there some variation of this that is possible?
    s
    r
    k
    • 4
    • 21
  • f

    Fabio

    09/10/2020, 5:56 AM
    Often when I try to handle generics I run into this casting situation, which generates a warning that I don't understand:
    abstract class TodayDynamicModel<V : View> : BardeenListUiModel() {
    
        fun asTodayDynamicModel() = this as TodayDynamicModel<View> // --> Unchecked cast: TodayDynamicModel<V> to TodayDynamicModel<View>
    }
    Isn't
    V
    guaranteed to be of type
    View
    ?
    k
    • 2
    • 4
  • e

    Erik

    09/10/2020, 7:28 AM
    https://github.com/jetbrains/kotlin/releases/tag/v1.4.10 has just been released, but the release name on GitHub is incorrect: it says 'v1.4.0' I already notified @stanislav.erokhin about this (he made the release), but I'm not sure if he reads messages on this Slack environment. @JB
    ✅ 1
    s
    o
    +4
    • 7
    • 39
  • l

    louiscad

    09/10/2020, 12:19 PM
    Congratulations on :kotlin: 1.4.10 release, new inference is on point! 👌
    ❤️ 1
    😌 1
    a
    • 2
    • 3
  • c

    CFrei

    09/10/2020, 1:00 PM
    Help needed - I get an exception: org.jetbrains.kotlin.codegen.CompilationException: Back-end (JVM) Internal error: wrong bytecode generated Boiled it down to that piece of code:
    fun s() = sequence<Unit> {
        suspend fun f() {
            let { f() }
        }
    }
    Anyone an idea how I need to replace/annotate
    let
    to get that up and running? (bug is in 1.4.0 and 1.4.10) - Thanks in advance!!!
    a
    • 2
    • 2
  • m

    msink

    09/10/2020, 1:43 PM
    So really 1.4.10 is a patch release - libraries compiled by 1.4.0 are comatible with 1.4.10, and vice versa.
    l
    • 2
    • 1
  • e

    Elka

    09/10/2020, 5:14 PM
    Hello, It looks like Kotlin 1.4 is bringing new stuff! Any reason why this breaking change? Code
    fun <T: Any> x(block: () -> T?) {
        val res = block()
        if (res != null) {
            println(res)
        } else {
            println("returned null")
        }
    }
    
    fun test() {
        x<Unit> {
            null
        }
    }
    Kotlin (<1.4) It prints "retuned null Kotlin (>= 1.4) It prints kotlin.Unit The weird part is that
    x { null }
    works fine. The compiler is able to infer the type to
    Unit?
    . But I can't write
    x<Unit?> { null }
    as the type is bound to
    Any
    which is something we need on Kotlin-Native (unless this has changed too)
    t
    i
    • 3
    • 3
  • b

    Brian Dilley

    09/10/2020, 6:43 PM
    isn’t that like
    void
    l
    s
    +2
    • 5
    • 12
  • l

    LastExceed

    09/10/2020, 7:08 PM
    fun <T> T.foo(param: T) {
    
    }
    
    suspend fun main() {
    	val x = "text"
    	val y = 42
    	x.foo(y)
    }
    why does this code compile and run successfully even though x and y are of different types ?
    s
    t
    i
    • 4
    • 8
  • d

    Don Phillips

    09/10/2020, 7:08 PM
    Hey everyone. So we just upgraded our Android project to Kotlin 1.4, and things have gone OK. But we're having this strange problem with Android Studio. The project builds fine, but it's saying it can't find the StringBuilder.appendLine() extension. Yet it sees that StringBuilder.appendLn() is deprecated. and if I ctrl+click into appendLn(), the StringBuilderJVM.kt file clearly has appendLine() in it. Anyone have any ideas? Using AS 4.0.1 and the latest Kotlin plugin, 1.4.10-release-Studio4.0-1 from today.
    s
    a
    • 3
    • 6
  • s

    SrSouza

    09/10/2020, 11:39 PM
    Hi folks, the Kotlin 1.4.10 is not yet publish at JCenter?
    a
    • 2
    • 1
  • s

    Slackbot

    09/11/2020, 5:32 AM
    This message was deleted.
    🚫 1
    l
    • 2
    • 2
  • s

    scottyab

    09/11/2020, 9:14 AM
    👋 Is it possible to use @OptIn (i.e
    @OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class
    ) for an entire package and sub packages? I’m getting a bunch of warnings in my tests as I’m using experimental
    runBlockingTest{}
    and I’d love to squash all the warnings in a single place rather than per class annotation.
    u
    • 2
    • 1
  • s

    Stephan Schroeder

    09/11/2020, 10:47 AM
    Since Kotlin 1.4 no longer requires stating the stdlib-dependency in my gradle.kts-file, I removed it, but somehow gradle now imports the 1.3 version of kotlin-reflect (I don't even need kotlin-reflect).
    > Task :module-ui:compileTestKotlin
    w: Runtime JAR files in the classpath should have the same version. These files were found in the classpath:
        /home/stephan/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-jdk8/1.4.10/998caa30623f73223194a8b657abd2baec4880ea/kotlin-stdlib-jdk8-1.4.10.jar (version 1.4)
        /home/stephan/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-jdk7/1.4.10/30e46450b0bb3dbf43898d2f461be4a942784780/kotlin-stdlib-jdk7-1.4.10.jar (version 1.4)
        /home/stephan/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-reflect/1.3.0/6fd129fd9ba8581f2cb9c58bfd431dda4ee0457e/kotlin-reflect-1.3.0.jar (version 1.3)
        /home/stephan/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.4.10/ea29e063d2bbe695be13e9d044dcfb0c7add398e/kotlin-stdlib-1.4.10.jar (version 1.4)
        /home/stephan/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-common/1.4.10/6229be3465805c99db1142ad75e6c6ddeac0b04c/kotlin-stdlib-common-1.4.10.jar (version 1.4)
    w: Consider providing an explicit dependency on kotlin-reflect 1.4 to prevent strange errors
    w: Some runtime JAR files in the classpath have an incompatible version. Consider removing them from the classpath
    a
    r
    • 3
    • 7
  • m

    Mike Patterson

    09/11/2020, 2:06 PM
    hey, apologies if this has been answered a thousand times, but is there a preferred solution to handling multiple listeners that can be added to one class?
    private class MyAdListener(private val listeners: List<AdListener>) : AdListener {
        override fun onAdStarted(adInfo: AdInfo) = listeners.forEach { it.onAdStarted(adInfo) }
        override fun onAdCompleted(adInfo: AdInfo) = listeners.forEach { it.onAdCompleted(adInfo) }
        override fun onAdBreakStarted(adInfo: AdInfo) = listeners.forEach { it.onAdBreakStarted(adInfo) }
        override fun onAdBreakCompleted(adInfo: AdInfo) = listeners.forEach { it.onAdBreakCompleted(adInfo) }
        // etc
    is there a method for delegation that works? something like...
    private class MyAdListener(private val listeners: List<AdListener>) : AdListener by listeners.each()
    z
    j
    • 3
    • 3
  • r

    ribesg

    09/11/2020, 2:28 PM
    [Unsolved] I’m upgrading a project from 1.3.61 to 1.4.10 and IDEA keeps saying that Kotlin isn’t configured in my buildSrc sources, what should I do ?
    n
    l
    s
    • 4
    • 62
  • l

    Logan Knight

    09/11/2020, 8:11 PM
    Not exactly kotlin, but does anyone know how to add a
    --compatibility
    flag to a Docker Compose run configuration in Ultimate?
    s
    • 2
    • 2
  • s

    Slackbot

    09/12/2020, 8:25 AM
    This message was deleted.
    d
    u
    • 3
    • 5
  • m

    Mehul Kabaria

    09/13/2020, 6:32 AM
    I want to create chatting application same like WhatsApp provides feature with video and audio calling. Any customized SDK available that give above all features. Your help would be appreciated.
    :stackoverflow: 6
    :google: 5
    k
    • 2
    • 2
  • h

    Harry

    09/13/2020, 11:37 AM
    I often thought why kotlin doesn't support 'lateinit val', as you know kotlin just has supported 'lateinit var'. My colleague sometime overwrite that variable what I defined. Is there whom can answer for that?
    m
    l
    +3
    • 6
    • 39
  • s

    Shashank

    09/13/2020, 1:55 PM
    Any idea what can I do to fix this? Or maybe I shouldn’t be using
    StateFlow
    at all? 🤔
    j
    • 2
    • 2
  • h

    harry.singh

    09/15/2020, 1:06 AM
    Hey all! Is there a way to run a Coroutine inside a lambda function?
    d
    i
    • 3
    • 9
  • k

    KayCee

    09/15/2020, 10:05 AM
    Is there any problem if I put too many functions (might be time consuming) in block init of viewmodel?
    s
    a
    • 3
    • 5
  • r

    rrva

    09/15/2020, 2:30 PM
    how can I rewrite code that looks like this to be more concise so that I avoid having to mention foo.someProperty twice?
    fun computeNewValue(msg: String, oldValue: String): String {
        return try {
        	println(msg)
            computeNewValue()
        } catch(e: RuntimeException) {
            oldValue
        }
    }
    
    fun main() {
        val foo = Foo()
        foo.someProperty = computeNewValue("Computing foo", foo.someProperty)    
    }
    n
    h
    +2
    • 5
    • 59
  • v

    v79

    09/15/2020, 7:19 PM
    Can I check, at runtime, if a cast is valid? I think I'd use a try...catch block in Java; is this the same approach in Kotlin?
    n
    • 2
    • 3
  • v

    Vladyslav Sitalo

    09/15/2020, 9:13 PM
    what’s the current best Kotlin vs Java article to point people to? 🙂
    j
    m
    s
    • 4
    • 3
  • a

    atara

    09/16/2020, 6:06 AM
    Hi. There is some interesting discussion in my team about the usage of the functions
    also
    (https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/also.html) and
    run
    (https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/run.html). I understand what they are doing but never had the need to use them. Do you use them at all? can you tell a valid case where you used them and it made your code better?
    p
    m
    +4
    • 7
    • 42
  • a

    andylamax

    09/16/2020, 8:04 AM
    can Libs compiled from 1.4 Mpp Project (android,jvm and js only) be dependencies in 1.3 Mpp (android,jvm and js only) ? I want to know if gradual migration is an option that I have,. By start migrating to some internal libs to 1.4 and move the whole to 1.4 after updating all the dependencies. This might take a while
    l
    • 2
    • 2
  • a

    Ashish Lama

    09/16/2020, 11:42 AM
    Can anyone point me to good articles regarding interfaces? I’m finding it pretty difficult to grasp the concept !! 😫
    l
    j
    • 3
    • 4
Powered by Linen
Title
a

Ashish Lama

09/16/2020, 11:42 AM
Can anyone point me to good articles regarding interfaces? I’m finding it pretty difficult to grasp the concept !! 😫
l

louiscad

09/16/2020, 12:49 PM
Do you want to use `interface`s for a specific use case? Also, did you check the `interface`s provided in the stdlib like
List
,
Comparator
and more? There are use cases for interfaces beyond that, but it might help you.
j

Jordi Pradel

09/16/2020, 1:14 PM
I assume the question is about the concept of interface in a class based language. Unforntunately, kotlin documentation for interfaces (https://kotlinlang.org/docs/reference/interfaces.html) assumes you already know what an interface is. As to grasping this context I would start by learning what a Java interface is (e.g. https://docs.oracle.com/javase/tutorial/java/concepts/interface.html, https://www.tutorialspoint.com/java/java_interfaces.htm or corresponding chapter in http://www.dblab.ntua.gr/~gtsat/collection/Java%20books/Bruce.Eckel.Thinking.In.Java.4th.Edition.Dec.2007.eBook-BBL.pdf ). And then, you can learn how kotlin interfaces are much more powerful. Note there may be better explanations (than those example links I provide) about what a Java interface is.
a

Ashish Lama

09/16/2020, 4:23 PM
Thank you for your responses ! Yes I’m just starting out so I just wanted to know what makes interfaces special and use cases when an interface is more relevant rather than a class or an abstract class. I’ll go through the links that you have provided. Thanks for hearing me out !
l

louiscad

09/17/2020, 6:49 AM
abstract classes can hold a state (have fields), but you lose the ability to to inherit another class at implementation site.
View count: 2