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

    darkmoon_uk

    03/12/2020, 2:04 AM
    Java-interop is bleeding:
    > Task :Examples:ExampleApplication:processDebugAnnotationsWithJavac FAILED
    error: cannot access JvmStatic
      class file for kotlin.jvm.JvmStatic not found
      Consult the following stack trace for details.
      com.sun.tools.javac.code.Symbol$CompletionFailure: class file for kotlin.jvm.JvmStatic not found
    1 error
    When compiling Java referencing a Kotlin library, is it expected that the annotation processor requires Kotlin annotations to be defined?
    • 1
    • 1
  • j

    Joan Colmenero

    03/12/2020, 10:52 AM
    Is there any other idiomatic way to do this?
    return when {
            pojo.dog != null -> {
                mapDog(this, pojo = pojo.dog!!)
            }
            <http://pojo.cat|pojo.cat> != null -> {
                mapCat(this, pojo = <http://pojo.cat|pojo.cat>!!)
            }
            else -> {
                mapDefault(this)
            }
        }
    I have to add the
    !!
    because otherwise is saying
    Smart cast is impossible because pojo.cat is an API class that is on a different module
    d
    m
    +2
    • 5
    • 13
  • a

    Achraf Amil

    03/12/2020, 2:16 PM
    How expensive are coroutines? 🤔 I mean, what might go wrong if I do something like this?
    d
    f
    e
    • 4
    • 10
  • i

    iseki

    03/12/2020, 2:50 PM
    Hi, How to get reified type parameter nested? I means
    inline fun <reified T: Any> awsl()
    If I use it as:
    awsl<List<String>>()
    How to get the
    String
    from
    T
    ? Thank you
    e
    k
    • 3
    • 4
  • c

    Chills

    03/12/2020, 3:33 PM
    how will i run kotlinc command by using gradle-kotlin plugin
    e
    • 2
    • 6
  • s

    Sam Garfinkel

    03/12/2020, 4:18 PM
    Are there ways to restrict the scope of a particular extension function to where
    this
    is not the receiver? For instance, if I’m wrapping an existing API in a DSL, a
    fun String.bar()
    might behave differently depending on what
    this
    is. I’m not able to subclass the wrapped API data type as it’s final, and inline classes don’t provide delegates apparently. I thought this would work:
    class Foo {
      fun doSomething(it: String) {
        // TODO
      }
    }
    
    inline class Bar(val foo: Foo) {
      fun String.doSomething() = doSomething(this)
    }
    But
    Bar(Foo()).doSomething("hello")
    is undefined.
    b
    e
    • 3
    • 5
  • g

    Gavin Ray

    03/12/2020, 5:10 PM
    Just wanted to share something that really impressed me about Kotlin having only worked with the language a few days: I was told to add a semi-complex date sort to an array of event objects, where events that have not happened yet are first, in ascending order. Then, events that are currently happening, followed by events that have already happened. Started to write a write a really janky lambda custom comparator, but then I found out you can just do this:
    val sortedGames = games.sortedWith(compareBy{
        Instant.now().compareTo(it.dateTime)
    })
    e
    j
    • 3
    • 36
  • d

    dazza5000

    03/12/2020, 6:30 PM
    noob here - why don't these match?
    m
    s
    a
    • 4
    • 17
  • c

    Chills

    03/13/2020, 4:34 AM
    well i have a kotlin project and it uses gradle-kotlin plugin the thing it uses this plugin to run / configure kotlin not sure what is the equivalent of kotlinc via this as CI fails to run
    kotlinc
    command
    z
    • 2
    • 9
  • x

    xii

    03/13/2020, 11:37 AM
    is there a method in List that allows me to do that easily? Thanks
    j
    • 2
    • 1
  • a

    Alexander Eckert

    03/13/2020, 1:30 PM
    Hi, i have some problem with generic types. Maybe someone can help me out. Consider the following code
    class MyClass<T : Comparable<T>> {
        fun join(other : MyClass<T>) {
            // so some stuff
        }
    }
    
    fun someFunction(a : Any, b : Any) {
        val instanceA = a as MyClass<*>
        val instanceB = b as MyClass<*>
    
        instanceA.join(instanceB)
    }
    s
    a
    k
    • 4
    • 4
  • l

    LastExceed

    03/13/2020, 4:03 PM
    is there a formatting convention for where to put the companion object inside a class?
    j
    • 2
    • 3
  • c

    Chills

    03/13/2020, 5:45 PM
    want to contribute to kotlin projects please let me know right channel for this.
    k
    l
    j
    • 4
    • 3
  • r

    ritesh

    03/13/2020, 10:56 PM
    Anyone faced this issue, where api response is not static but dynamic, like in case of 200 it will give full json response body and in case of 304 empty body and then there moshi throws exception. If anyone dealt with it before, I know we can write convertor but is there any other way.
    k
    t
    • 3
    • 5
  • i

    iseki

    03/14/2020, 9:29 AM
    Hi, I have a problem. IDE tell me my lambda 's parameter type is wrong. But the expected type is same as I wrote... I don't know what's wrong with me...
    k
    • 2
    • 1
  • i

    iseki

    03/14/2020, 9:31 AM
    In my calc, the type I wrote is correct... Same as IDE & compiler 's expected. I try to enable/disable the new type inference algorithm, but useless...
    l
    • 2
    • 4
  • s

    sultanofcardio

    03/14/2020, 7:06 PM
    When extension functions are used in Java, they're represented as the method name taking the receiver as a parameter. How can I specify the name of this parameter in Java? Currently it shows up in IntelliJ as
    $this$methodName
    h
    • 2
    • 2
  • c

    codec

    03/14/2020, 10:37 PM
    How does one create default implementations of interface methods like in Java, but with Kotlin? For example:
    interface Example {
    default public void method(){
    println("how default method implementations look in java, using the keyword default"
    }
    s
    s
    • 3
    • 5
  • t

    Travis Griggs

    03/15/2020, 5:03 AM
    (Simple?) question? I want to create a generic subclass of RecyclerView.Adapter<RecyclerView.ViewHolder>. I tried:
    class FungibleRecyclerAdapter<T : RecyclerView.ViewHolder>():RecyclerView.Adapter<RecyclerView.ViewHolder>() {
       override fun onCreateViewHolder(parent:ViewGroup, viewType:Int):T {
          return RecyclerView.ViewHolder()
       }
    
       override fun getItemCount():Int {
          return 1
       }
    
       override fun onBindViewHolder(holder:T, position:Int) {
       }
    }
    I got a couple of errors: 1. It complains that FungibleRecyclerAdatper is not a proper subclass because it doesn’t implement all of the methods it should 2. It complains that it does not know what the return ViewHolder() is not what it wants, a T 3. It complains that the onBindViewHolder doesn’t override any method I can make a “normal” subclass of the Adapter where I specify a specific subclass of RecyclerView.ViewHolder if I want:
    MyAdapter() : RecyclerView.Adapter<MyRowViewClass>() { … }
    where
    MyRowViewClass
    is a subclass of
    ViewHolder
    just fine. But I wanted to make a generically pluggable one.
    l
    k
    • 3
    • 5
  • t

    Travis Griggs

    03/15/2020, 5:04 AM
    FWIW, the document on generics didn’t help at all. It’s all about how Java did it wrong. I’m coming to Kotlin from the Swift world and don’t care about Java.
    ➕ 1
    😂 1
    s
    • 2
    • 3
  • s

    steenooo

    03/15/2020, 3:59 PM
    I'm trying to provide a default implementation for a Java interface, It contains one value, with a getter and a setter. (isCancelled() & setCancelled(boolean)) When i try to implement this in Kotlin, it does not allow me to convert this into a property, when I add another property for this, it will generate additional getters and setters
    • 1
    • 1
  • g

    Gilberto Diaz

    03/16/2020, 2:29 AM
    Hi to all! Who has read any of these books and which one do you recommend. (I’m not into Android) https://kotlinlang.org/docs/books.html
    m
    m
    +2
    • 5
    • 11
  • p

    pavi2410

    03/16/2020, 2:59 AM
    Is StateFlow/EventFlow as talked about in a KotlinConf talk by @elizarov implemented? If yes, is it stable?
    k
    e
    • 3
    • 2
  • c

    Colton Idle

    03/16/2020, 3:22 AM
    Just curious if anyone from the JB team can shed light on why IntelliJ default auto-format for code in kotlin is different than the Kotlin code style guidelines from JB? I know there is an article that talks about the differences, but I'm more curious why JB doesn't just align them?
    👍 7
    m
    • 2
    • 1
  • s

    Slackbot

    03/16/2020, 3:57 AM
    This message was deleted.
    c
    t
    • 3
    • 3
  • l

    LastExceed

    03/16/2020, 11:20 AM
    why doesnt
    List
    inherit from
    Set
    ? isn't a Set the same thing as a List but without indexing?
    t
    e
    +4
    • 7
    • 14
  • b

    Borja

    03/16/2020, 12:00 PM
    Does anyone can help? I’ve published a mpp library to bintray, gradle finds it but does not resolve the package in base module. But if i append -jvm to the module it resolves the package.
    t
    m
    • 3
    • 8
  • r

    ritesh

    03/16/2020, 12:06 PM
    How would you copy the whole content of class A to class B data class A(val name:String, val list:List<A>) to data class B(val name:String, val list:List<B>)
    l
    a
    h
    • 4
    • 16
  • m

    Max

    03/16/2020, 12:31 PM
    Hello everyone, I’ve got kind of a noob question: how do you properly log (or just print) inside a test? specifially if it runs with gradle?
    f
    • 2
    • 6
  • p

    Piasy

    03/16/2020, 1:34 PM
    Hi guys, I'm wondering if the
    ?.
    operator thread safe? for example I have a
    var str: String? = null
    , is it safe to call
    str?.length
    ? could NPE happen?
    j
    s
    a
    • 4
    • 7
Powered by Linen
Title
p

Piasy

03/16/2020, 1:34 PM
Hi guys, I'm wondering if the
?.
operator thread safe? for example I have a
var str: String? = null
, is it safe to call
str?.length
? could NPE happen?
j

jw

03/16/2020, 1:37 PM
It's equivalent to
val localStr = str
if (localStr != null) {
  localStr.length
}
s

streetsofboston

03/16/2020, 1:37 PM
The ‘call’ to
?.
itself is ‘thread-safe’/‘atomic’. But afer that call, the global/instance variable you called it on, can be null again.
p

Piasy

03/16/2020, 1:40 PM
crystal clear, thanks!
a

Aditya

03/16/2020, 5:45 PM
a
let
on that would resolve the thread safety issue, right? My understanding is that
str?.let {
it.length
}
would be safe since the
it
is a copy
j

jw

03/16/2020, 5:50 PM
there is no issue. that code is equivalent to
str?.length
👍 1
a

Aditya

03/16/2020, 6:40 PM
Huh, I honestly thought
let
made a local copy. Thanks!
Right, I see now
j

jw

03/16/2020, 6:46 PM
It does. So does
str?.length
. That was my point.
👍 2
View count: 2