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

    Lulu

    11/18/2020, 8:47 PM
    If I have a
    MutableList
    , and I expose it to another function with just the
    List
    interface, can that function cast it to
    MutableList
    ? At what point is something cast-able to its original type? I'm not new to generics but I haven't thought about that before.
    e
    s
    +2
    • 5
    • 85
  • i

    igor.wojda

    11/19/2020, 8:51 AM
    Hey In my library (that will be distributed) I have simple Kotlin data class where some of the properties are nullable
    // Kotlin class
    data class Test(
    val name: String,
    val lastName :String?
    )
    // Generated Java class src
    public final data class Test public constructor(name: kotlin.String, lastName: kotlin.String) { 
    }
    Since this class will be also used from Java is there any way to include
    Nullable
    and
    NonNull
    annotations in generated the code for the properties? Something like this:
    // Generated Java class src
    public final data class Test public constructor(@NonNull name: kotlin.String, @Nullable lastName: kotlin.String) { 
    }
    (I mostly care about
    NonNull
    ): The main drive for this behaviours is that now I can create new instance of
    Test
    class from Java and pass null values
    new Test(null, null)
    without any indication that this is wrong/undesired usage- I believe with annotations IDE would display error/waring 🤔
    r
    r
    • 3
    • 4
  • b

    Bernhard

    11/19/2020, 9:39 AM
    is there a better way to divide ints and round down to an int than this
    floor(int1.toDouble() / int2).toInt()
    v
    r
    • 3
    • 12
  • m

    mzgreen

    11/19/2020, 11:41 AM
    congrats @elizarov 👏
    🎉 52
    ➕ 1
    e
    • 2
    • 1
  • e

    elizarov

    11/19/2020, 12:45 PM
    The Kotlin community is what drives the Kotlin project forward and you are the ones who we, in the Kotlin team, all ultimately work for. Thank you for your love for Kotlin ❤️
    :kotlin-intensifies: 18
    :kotlin: 19
    ❤️ 68
    👏🏼 42
    🙏🏼 13
    a
    a
    +2
    • 5
    • 4
  • d

    dewildte

    11/19/2020, 5:03 PM
    Is there a Kotlin equivalent for a Swift
    DispatchQue
    ? I have a
    MutableMap<T,T>
    and I want to make operations done to it thread safe. So far I just wrap accesses to it in:
    val lock = Any()
    synchronised(lock) { map.doTheThing() }
    Is this fine?
    m
    n
    • 3
    • 8
  • n

    Ncrnomarkovic

    11/19/2020, 5:08 PM
    First time creating an Android project in 4.2. I have a MainActivity in which I want to immediately launch another Activity tht I created that has my Compose views built. I know I can change the Manifest file, but I want to understand how to move from MainActivity to another activity. THANK YOU IN ADVANCE
    Untitled
    c
    e
    • 3
    • 2
  • s

    StavFX

    11/19/2020, 5:51 PM
    In this example
    foo
    is not being smart casted to non-null even tho it cannot be null in that else block. Shouldn’t the compiler be smart enough to infer this?
    class Foo(val str: String? = "")
    val foo: Foo? = null
    if (foo?.str.isNullOrBlank()) {
       //...
    } else {
       println(foo.str) // doesn't compile. foo is not smart-casted to a non-null type
    }
    Btw if the condition was
    foo?.str == null
    , this would compile. so it’s not impossible to “backtrack” from
    str
    to
    foo
    in this case
    j
    n
    • 3
    • 33
  • d

    dimsuz

    11/20/2020, 12:18 AM
    Hi! I have a nullable
    val property: String?
    in
    src/main
    and then in
    src/test
    I have
    if (property != null) property.length
    and this gives an error that
    property
    cannot be smart-casted, because it's defined in a different module. But it's the same [gradle] module! Also I have just moved this class + its test from another module where there was no such error. Why can this happen? Drives me nuts. Tried to compile outside an IDE from console, same errors...
    t
    n
    +2
    • 5
    • 11
  • u

    ursus

    11/20/2020, 5:00 AM
    Naming issue help I have a shared library across multiple apps. Lets say it provides
    Authenticator
    class which has
    login()
    function Now, app B needs logout as well, so it extends Authenticator and adds
    logout()
    functionality. How would you call such type? 1) If I were to take inspiration from the way List interface looks I'd call it
    LogoutableAuthenticator
    But such name looks silly for app B developers, as they only use a
    Authenticator
    which has both login and logout functions 1.5) Give it app name prefix like
    AppBAuthenticator
    . Also feels silly. All app B types then should have this prefix 2) Or, design it backwards, leave leaf implementations prefixless, so app B has Authenticator but it extends
    BaseAuthenticator
    . But this again looks stupid in shared code 3) Name them both Authenticator and let just the package name be different
    n
    k
    v
    • 4
    • 5
  • a

    Animesh Sahu

    11/20/2020, 5:48 AM
    If somebody is online to help 😅😁: https://stackoverflow.com/q/64924393/11377112
    n
    n
    • 3
    • 56
  • a

    Animesh Sahu

    11/20/2020, 8:04 AM
    What's the correct way to instantiate a class using the reflections (KClass) with primary constructor? Assuming we know the type and number of parameters.
    v
    • 2
    • 3
  • r

    Raid Xu

    11/20/2020, 8:39 AM
    Is is a good idea to use
    GlobalScope
    in a singleton class?
    a
    • 2
    • 4
  • b

    Ben Hammond

    11/20/2020, 12:04 PM
    do Closures close over Sychronized monitor blocks? I declare a
    FixedThreadPool
    executor within a synchronized block, and I pass in a ThreadFactory Lambda fun; when the threadfactory function is subsequently evaluated, will the Monitor lock be acquired? (I am hoping/expecting not - otherwise I would put the sychronized inside the ThreadFactory fn)
    c
    • 2
    • 12
  • m

    mattinger

    11/20/2020, 7:17 PM
    Hi All. I’m trying to write a KSP compiler plugin. The problem is that i need to depend on classes that live in .aar files (ie, firebase-config). Is this even possible? I’m adding the dependency to my annotations module which has the kotlin.jvm plugin setup. However, because it’s an aar, i can’t import the classes. Anyone ever tried to do this before?
    b
    • 2
    • 1
  • p

    Pratik Tandel

    11/20/2020, 8:19 PM
    Hey folks, we’re using Kotest and was wondering if there’s a library or a way of writing fixtures that other folks seem to like and enjoy?
    r
    s
    • 3
    • 5
  • a

    akuleshov7

    11/20/2020, 11:34 PM
    Hi all, diktat 0.1.5 was released. Diktat is awesome kotlin linter with hundreds of inspections (unique inspections, that are missing in ktlint and detekt) and autofixers, and it’s own strict code style codestyle. And we need your help! We need stars on github to attract more developers to the project. Please support us with your star: https://github.com/cqfn/diKTat
    ❤️ 1
    👍 2
    ⭐ 1
    😠 7
    👎 12
    a
    e
    v
    • 4
    • 3
  • c

    Colton Idle

    11/21/2020, 3:05 AM
    This just feels spammy.
    ➕ 5
    n
    • 2
    • 1
  • l

    liminal

    11/21/2020, 3:08 AM
    i have a Builder class with a bunch of functions for various types
    .with(key: String, value: String)
    ,
    .with(key: String, value: Boolean)
    . i'd like to be able to call these functions conditionally on the call site. i can't think of anything better than simply adding a param
    .with(key: String, value: String, shouldUse: Boolean)
    to determine if a value should be used internally by the builder if
    shouldUse
    is true
    n
    • 2
    • 10
  • a

    Animesh Sahu

    11/21/2020, 10:16 AM
    Is there a way to make this type of inference work?
    j
    t
    +3
    • 6
    • 30
  • t

    Tolriq

    11/21/2020, 3:47 PM
    Le canal saint martin?
    🤣 5
    n
    • 2
    • 1
  • n

    Nikky

    11/22/2020, 3:11 AM
    is there a cleaner way to do this?
    val filteredMap: Map<String, B> = someMap.filterValues { value -> value is B } as Map<String, B>
    because the unchecked cast is marked as warning
    n
    n
    v
    • 4
    • 11
  • f

    frank

    11/22/2020, 6:36 AM
    I'm using latest version of Intellij EAP, but the IDE can't find the latest version of kotlin(1.4.20) plugin. version found: 1.4.10 (203-1.4.10-release-IJ5981.20) Do I have to manually install the plugin or add any repository?
    p
    m
    • 3
    • 4
  • a

    Alexander Karkossa

    11/23/2020, 1:00 AM
    Hey guys, is it possible, that data class could generate StackOverFlow while compiling? After implementing the OpenApiGenerator for a strange Server API, i got multiple generated data class with > 200 fields. Don't ask for the reason of such apis... No possibility to change it for now... While app:compileDebugKotlin a crash... Haven't seen such an error before. Any ideas? Kotlin 1.4.10 with enabled Jetpack Compose for Android I replaced the classname with xx.xxxx.xxxx.xxxx.xx.xxxx.Xxxxx
    d
    u
    • 3
    • 3
  • h

    hoang

    11/23/2020, 10:57 AM
    hello, anyone has fail compile issue with the new
    kotlin 1.4.20
    and
    Optional<V>
    Type mismatch: inferred type is Optional<V!!> but Optional<V> was expected
    the same code compile ok in
    kotlin 1.4.10
    fun first(vPredicate: (V) -> Boolean): Optional<V> {
            val v: V? = firstOrNull(vPredicate)
            return if (v == null) {
                Optional.empty()
            } else {
                Optional.of(v)
            }
        }
    the errored line is the one that return
    Optional.empty()
    v
    • 2
    • 3
  • u

    user

    11/23/2020, 11:33 AM
    Kotlin 1.4.20 Released Kotlin 1.4.20 is here with new experimental features for you to try. Being open to community feedback is one of the Kotlin team’s basic principles, and we need your thoughts about the prototypes of the new features. Give them a try and share your feedback on Slack (get an invite here) or YouTrack. Here are […]
    🎉 6
    :kotlin-intensifies: 19
    a
    a
    +2
    • 4
    • 5
  • b

    Bernhard

    11/23/2020, 11:39 AM
    when using:
    fun XMLStreamWriter.writeStuff(action: XMLStreamWriter.() -> Unit)
    how does Kotlin know where it takes the this from?
    m
    • 2
    • 2
  • s

    Slackbot

    11/23/2020, 1:52 PM
    This message was deleted.
    n
    • 2
    • 1
  • a

    Alexander Black

    11/23/2020, 5:13 PM
    is there a way to use a kotlin flow retry operator without the source throwing an exception? as in if the source returns some value retry the original flow
    r
    e
    • 3
    • 3
  • n

    Ncrnomarkovic

    11/23/2020, 5:48 PM
    How do you create a calculated property for this model? New to Kotlin
    Untitled
    h
    n
    • 3
    • 5
Powered by Linen
Title
n

Ncrnomarkovic

11/23/2020, 5:48 PM
How do you create a calculated property for this model? New to Kotlin
Untitled
h

hoang

11/23/2020, 5:52 PM
I often use extension function for such purpose, something like
fun Merchant.loc() = this.latitude to this.longitude
sorry if there’s any syntax error, I just type it out
n

nanodeath

11/23/2020, 5:59 PM
that is...a lot of fields. anyway, just say
val loc get() = latitude to longitude
also you might like #getting-started
n

Ncrnomarkovic

11/23/2020, 6:04 PM
Thank you - I was setting up the model incorrectly. I figured it out!
n

nanodeath

11/23/2020, 6:07 PM
btw if that's java.util.Date, I'd suggest using java.time.Instant instead -- easier to work with, and not mutable. and generally more modern.
View count: 2