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

    E.Kisaragi

    05/13/2020, 7:43 PM
    why can't I do some like
    typealias Nullable<T> = T?
    (it's kinda useless, but I have to do similar one)
    s
    s
    • 3
    • 4
  • a

    Animesh Sahu

    05/14/2020, 7:39 AM
    https://stackoverflow.com/q/61791680/11377112 somebody know how should I approach this? How should you tackle situation where there is multiple interfaces decalre default parameters in their function decalration. I've also put a reproducible example corresponding to kotlin playground in there.
    s
    • 2
    • 8
  • s

    Stefan Beyer

    05/14/2020, 8:50 AM
    Hi there 🙂 Do you guys know how to deprecate stuff that is not easily replaceable with another line of code? I am cleaning up a legacy app and there are a few constructs that I want to phase out. Example: Lets say I have an app where the old way of accessing the database is done via type-unsafe results like
    Array<Any?>
    . There are helper methods that type juggle the return values like so:
    fun Any?.toIntOrZero(): Int = (this as? Long)?.toInt() ?: (this as? Double)?.toInt() ?: 0
    This is of course horrible, so I want to deprecate all of these helper functions, so that any developer sees that this is not the right way to do things:
    @Deprecated("use the new type safe db repository instead of this manual type-juggling (see readme.md for refactoring advice)")
    fun Any?.toIntOrZero(): Int = (this as? Long)?.toInt() ?: (this as? Double)?.toInt() ?: 0
    But now there is a warning, that the replaceWith parameter of the deprecation annotation is missing. But there is no simple replacement of this, since every database access needs to be refactored and cleaned up individually. this helper function is just a symptom of the problem, so to speak. So is this ok, just to slap a
    @Suppress("DeprecatedCallableAddReplaceWith")
    in front of this deprecation annotation and call it a day? Or is there a better way to do this?
    i
    s
    • 3
    • 4
  • j

    Jakub

    05/14/2020, 1:34 PM
    Is there sth in kotlin to map all counties special characters like:
    ó
    ->
    o
    ł
    ->
    l
    I can’t see in
    contains
    function to ignore them.
    d
    • 2
    • 2
  • g

    Gabriel Feo

    05/15/2020, 11:10 AM
    Does anyone know whether the new slack app is native?
    s
    s
    • 3
    • 5
  • r

    rrva

    05/15/2020, 1:03 PM
    if I want to create enums from strings, where the string value does not correspond to the enum name, and I want to scope the function to the enum, and I do not want to create an extension function on
    String?
    , what is idiomatic kotlin for it?
    b
    a
    m
    • 4
    • 3
  • s

    Stephan Schroeder

    05/15/2020, 3:49 PM
    How do I tear off a function from the companion object of a class? I'd expect this to work:
    fun main() {
        listOf("yes", "no").filter(SomeClass::filterYes).forEach{
            println(it)
        }
    }
    
    class SomeClass {
        companion object {
            fun filterYes(str: String) = str == "yes"
        }
    }
    but it doesn't. You can try it here: https://pl.kotl.in/sHlk5l7V0
    r
    • 2
    • 2
  • a

    asad.awadia

    05/15/2020, 4:28 PM
    Is anyone using kotlin for non-android, non-js, non rest-api style dev work?
    🤘🏻 1
    c
    p
    +2
    • 5
    • 7
  • k

    Kevin Gorham

    05/15/2020, 5:30 PM
    can anyone think of a more idiomatic way to do this?
    if (error != null) throw error
    c
    j
    +3
    • 6
    • 7
  • e

    egorand

    05/15/2020, 6:09 PM
    How are people working around
    Please set environment variable JDK_16 to point to JDK 1.6 installation
    Kotlin repo build failure on macOS Catalina?
    a
    • 2
    • 2
  • m

    mathew murphy

    05/15/2020, 6:23 PM
    Something is still using JDK 1.6?
    s
    • 2
    • 1
  • h

    Hamza

    05/16/2020, 9:40 AM
    If I have a KProperty of
    KProperty<Type, Field>
    , is it possible for me to get the specified field of
    Type
    ? so like if we have
    data class Foo(bar: String)
    
    fun main() {
      val foo = Foo("baz")
      val whatWeWant = Foo::bar
      println(foo.(whatWeWant)) // prints baz. unknown syntax
    }
    c
    • 2
    • 2
  • m

    Merileoke

    05/16/2020, 11:16 AM
    i thought android studio is more advanced, has more flexibility/tools
    👍 3
    s
    j
    m
    • 4
    • 7
  • h

    Hanhnguyen Qcqa

    05/16/2020, 11:19 AM
    Because people use Mac more than Windows, do they? 🙄
    j
    • 2
    • 2
  • s

    Sourabh Rawat

    05/16/2020, 2:58 PM
    is following possible in kotlin? if not, what will be the kotlin way to do it
    while (isalnum(LastChar = getchar())) {
        IdentifierStr += LastChar;
    }
    taken from c++.
    🇳🇴 2
    m
    e
    • 3
    • 3
  • r

    Rodrigo Silva

    05/16/2020, 5:31 PM
    Sem título
    r
    • 2
    • 13
  • c

    chan

    05/17/2020, 8:06 AM
    Does anyone know what does this task
    inspectClassesForKotlinIC
    do for Kotlin in Gradle?
    e
    • 2
    • 1
  • p

    Paul Woitaschek

    05/17/2020, 9:42 AM
    I find kotlins regex kind of complicated. Usually I use it to find the first match, but it seems I don't understand something about how it is supposed to work because the code looks kind of awkward:
    regex.find(line)?.groupValues?.getOrNull(1)
    Is this the correct way to find the first match of a regex result?
    a
    a
    +2
    • 5
    • 5
  • o

    Ofir Bar

    05/17/2020, 4:41 PM
    Any guys working on some cool open source project? I got fired and honestly I am bored as fuck, enough Call of Duty for me. 😂
    n
    • 2
    • 1
  • t

    tseisel

    05/17/2020, 7:07 PM
    I juste noticed that it's actually impossible to write the following:
    for (n in 0.0..1.0 step 0.1)
    I know that floating point numbers are imprecise and such a for loop should be avoided. Is this why there is no such construct ? Or are there any alternatives ?
    n
    • 2
    • 1
  • c

    crummy

    05/18/2020, 12:41 AM
    in general there are two common ways to handle errors: • throw exceptions, and catch them somewhere else • return a object that contains a successful value OR an error (e.g. with an
    Either<>
    class)
    💯 2
    j
    m
    • 3
    • 3
  • t

    turansky

    05/18/2020, 9:39 AM
    Operator
    component0
    compiled successfully. How it can be used as operator on practice? On playground
    component0.kt
    e
    s
    +2
    • 5
    • 7
  • b

    Ben

    05/18/2020, 12:28 PM
    Hello, I've got a sealed class like below and I'm struggling to use the Empty state in Java. Any advice? Calling
    State state = State.Empty;
    gives "expression expected". Am I missing something obvious?
    sealed class State() {
        object Empty : State()
    
        ...
    }
    d
    • 2
    • 2
  • j

    jean

    05/18/2020, 1:54 PM
    Any strong reasons to not do something like that
    private val channel = Channel<Event>()
    val events = flow { emit(channel.receive()) }
    Use the channel inside a class to expose an api to emit values and receive those values else where as a flow?
    a
    a
    • 3
    • 4
  • p

    Philipp Mayer

    05/18/2020, 4:08 PM
    Does anyone have recommendations to build a fast (graal native?) cli app with Kotlin? I'm looking for something to prompt the user with options (a select field) and don't want to use --parameters. Thanks! I thought about spinning up a quarkus command app and add some kind of cli framework to it (picocli, clikt) but it seems that those two frameworks work with supplied parameters and not with prompts.
    y
    • 2
    • 2
  • w

    william

    05/18/2020, 4:23 PM
    val a = Regex("foo")
    a.replace("foobar", "\\w")
    res3: kotlin.String = wbar
    i would expect this to return
    \\wbar
    - am I missing something here? Isn't
    \\
    an escape for the
    \
    ?
    n
    • 2
    • 2
  • a

    Adam Hurwitz

    05/18/2020, 7:01 PM
    What is the best way to stay up-to-date with the Kotlin
    StateFlow
    release developments? I see there was a merge #1974 into the develop branch for StateFlow recently. After the develop branch is merged into master, does that mean StateFlow may be available in the next alpha/beta release?
    z
    m
    o
    • 4
    • 5
  • r

    Rodrigo Silva

    05/18/2020, 7:25 PM
    Hi all. How do I pass the operator "!!" for all hashmap "keys" I need to do something linke this: hasOne = HashMap<String?, String> hashTwo = HashMap<String, String> hashTwo = hashOne
    s
    • 2
    • 8
  • l

    LeoColman

    05/18/2020, 7:26 PM
    Perhaps
    hashTwo = hashOne.map { (k, v ) -> k!! to v}
    :thread-please: 1
    👍 1
    r
    • 2
    • 1
  • r

    rrva

    05/19/2020, 12:17 PM
    If I have moved a class to a different gradle module, and I have code like so,
    if (it.playables != null && it.playables.isNotEmpty())
    Why does the compiler force me to write
    if (it.playables != null && it.playables!!.isNotEmpty())
    Due to the fact that
    Smart cast to 'List<IdString>' is impossible, because 'it.playables' is a public API property declared in different module
    d
    s
    +3
    • 6
    • 10
Powered by Linen
Title
r

rrva

05/19/2020, 12:17 PM
If I have moved a class to a different gradle module, and I have code like so,
if (it.playables != null && it.playables.isNotEmpty())
Why does the compiler force me to write
if (it.playables != null && it.playables!!.isNotEmpty())
Due to the fact that
Smart cast to 'List<IdString>' is impossible, because 'it.playables' is a public API property declared in different module
d

Desmond Teo

05/19/2020, 12:23 PM
Alternative will be
it.playables?.isNotEmpty() == true
s

streetsofboston

05/19/2020, 12:28 PM
Some other thread that may have access to the instance referred to by
it
may have changed `it`’s nullable property
playbable
to the value null just after the
it.playables != null
check but before the
it.playables.isNotEmpty()
call.
r

rrva

05/19/2020, 12:30 PM
but how does the nullability guarantees change due to the fact that I am modularizing the code?
it was able to do the smart cast when the code was local to the same module
s

streetsofboston

05/19/2020, 12:31 PM
Maybe the compiler could check if anything was changing the value to ‘null’ or not while it was in the same module…. I must admit, this is a bit mystifying 🙂
r

rrva

05/19/2020, 12:33 PM
I solved it by creating a locally scoped copy
s

streetsofboston

05/19/2020, 12:35 PM
yeah… usually doing something like
it.playables?.let { … }
will do the trick
m

Michael de Kaste

05/19/2020, 12:59 PM
I see this question coming up a lot of times
k

Kroppeb

05/19/2020, 1:52 PM
The reason is that each module can be compiled seperatly. Now this means that if you compile this snippet and then change
playables
to be a
var
instead of a
val
, or a inconsistent custom getter, this piece of code would still work, as there is still a getter available. However now the guarantee that
it.playables
is not null is no longer true.
➕ 3
k

kqr

05/20/2020, 9:56 AM
if my dependecy has changed, shouldn't we recompile too?
View count: 6