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

    voddan

    10/11/2018, 1:13 PM
    https://stackoverflow.com/questions/52760965/kts-script-in-a-grdale-project
    h
    • 2
    • 3
  • e

    Egor Trutenko

    10/11/2018, 1:48 PM
    You can also
    map
    it.
    doubleArray.map { it.toFloat() }
    UPD As was fairly noticed,
    map
    returns
    Array<Float>
    , which I overlooked. You need to additionally call
    toFloatArray()
    to avoid boxing
    👍 1
    d
    • 2
    • 1
  • i

    ivano

    10/11/2018, 5:03 PM
    what is the best practice strategy in kotlin/android to save an array and use it after?
    a
    • 2
    • 11
  • m

    matt tighe

    10/11/2018, 6:57 PM
    is there a guide somewhere to submitting a kotlin issue? i can’t seem to find one
    r
    • 2
    • 1
  • c

    CodeIsmail

    10/11/2018, 8:06 PM
    Hi, Can anyone explain what "backing field" is, with code sample. I'd appreciate it.
    v
    • 2
    • 5
  • b

    bitjedi

    10/12/2018, 5:47 AM
    Hi I want to run python scripts from the kotlin server which is written with spring boot ?? Scripts are written in python 3.0
    h
    • 2
    • 5
  • c

    CamilleBC

    10/12/2018, 9:10 AM
    I need to stop asking stupid question... I did not look the
    use()
    doc (https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/use.html)... Sorry!
    h
    • 2
    • 2
  • d

    diego-gomez-olvera

    10/12/2018, 10:06 AM
    you can also just use https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/java.io.-file/append-text.html
    c
    • 2
    • 5
  • v

    voddan

    10/12/2018, 11:58 AM
    Hello! If I were be to explain asynchronous programming to a newbie, what would be some simple examples of situations when a client actively need to do something useful while waiting for server/database? I want real-life-ish situations to drive my point. Keeping UI event loop responsive, what else?
    m
    m
    c
    • 4
    • 10
  • s

    Sam

    10/12/2018, 9:29 PM
    Is there a way to get reference to lambda in its definition?
    val lambda = { value : Int -> executor( value, this ) }
    s
    m
    g
    • 4
    • 15
  • k

    keturn

    10/13/2018, 4:28 AM
    oh yay I just got to my first "a mutable property that could have been changed by this time" error.
    h
    a
    +2
    • 5
    • 15
  • j

    jcechace

    10/14/2018, 8:58 AM
    Hello. Could somebody have a look at this
    build.gradle.kts
    and hint me how to make the project work with latest 1.3.rc and ktor 1.0.0.alpha-3?
    a
    n
    • 3
    • 5
  • n

    Nikky

    10/14/2018, 10:07 AM
    what is the difference between
    fun <T : BaseClass> doThing() : ReturnType
    and
    fun <T> doThing() : ReturnType where T : BaseClass
    ? is it just a matter of taste ?
    j
    a
    • 3
    • 3
  • n

    Nikky

    10/14/2018, 10:36 AM
    another question where i am not even sure where to begin searching.. is there a annotation that lets the user know that something else might be cleaner and replace code that is less severe than
    @Deprecated
    usecase is a DSL
    j
    s
    • 3
    • 8
  • o

    oday

    10/14/2018, 11:16 AM
    how can I check for the nullability of a variable that should contain a function?
    a
    s
    • 3
    • 17
  • s

    Shawn

    10/15/2018, 5:38 PM
    Hence all the fanfare around unsigned types in 1.3
    p
    • 2
    • 3
  • n

    Nikky

    10/15/2018, 8:02 PM
    @mruno

    https://www.youtube.com/watch?v=-y2vW94mBDE▾

    👍 1
    m
    t
    • 3
    • 9
  • s

    Shawn

    10/17/2018, 12:33 AM
    data classes do not have a different base class.
    v
    • 2
    • 2
  • p

    poohbar

    10/17/2018, 3:25 AM
    is this is how we are supposed to write a string to output stream?
    h
    • 2
    • 1
  • p

    poohbar

    10/17/2018, 12:24 PM
    how can i make a function with optional non nullable parameter which will use the default when i pass in null?
    s
    e
    +2
    • 5
    • 16
  • p

    poohbar

    10/17/2018, 12:24 PM
    fun foo(input: String = "default")...
    
    val s: String? = "hi"
    foo(s) // error
    am I missing something?
    m
    • 2
    • 1
  • p

    poohbar

    10/17/2018, 12:25 PM
    I know I could just omit the parameter but then I would have to do:
    if (s == null) foo() else foo(s)
    h
    a
    • 3
    • 6
  • u

    ursus

    10/19/2018, 3:42 AM
    but I feel weird having global fun createFooViewModel(...)
    k
    • 2
    • 1
  • m

    marcelo

    10/20/2018, 2:01 AM
    Getting an error on the
    tag
    within my
    start
    method saying:
    Required:
    Tag<T#1 (type parameter of com.revl.metrics.Timer1.start)>
    Found:
    Tag<T#2 (type parameter of com.revl.metrics.Timer1)>
    Timer1.kt
    k
    s
    • 3
    • 2
  • u

    ursus

    10/20/2018, 2:10 AM
    style guide says I should use classic for loop over forEach. I am paranoid android and dont want to instantiate iterators on arraylists, so, would creating a extension function forXYZ, which is just a for until loop, be considered antipattern?
    k
    d
    • 3
    • 6
  • o

    oday

    10/20/2018, 8:03 PM
    in programming generally, is it common to write a function that can accept more than 1 type of object?
    v
    a
    k
    • 4
    • 19
  • d

    deas_

    10/21/2018, 4:25 PM
    looking for where to get started if i was to use kotlin
    d
    • 2
    • 2
  • a

    alwyn

    10/21/2018, 7:40 PM
    If I define a lazy property with an initializer as in this code. Does initializer have access to X and if so how is it accessed?
    -.kt
    v
    a
    • 3
    • 17
  • s

    skneko

    10/22/2018, 1:37 PM
    Does Kotlin have any native way of working with an event driven architecture (like in C#, or Node.js EventEmitters)? I'm planning on making a library that, by definition, must be event driven and allow the user to define a set of handlers in response to the occurrence of a certain event. What would be the preferred way in Kotlin, or the best way according to your personal preferences? I'm thinking of defining an event class which has a list of handlers and then define a subscribe method to add the functions. I'm willing to implement this from scratch, but the main purpose of the library is not this one and I prefer to reuse an existing library if possible.
    s
    • 2
    • 7
  • s

    Steven McLaughlin

    10/22/2018, 5:47 PM
    This is a noob question and may have been asked before, but what does the when block do by default if a case is not found? Like in the following example, what is actually happening when I pass
    BAD
    into that function? Essentially, I want to make the when block exhaustive but not change the behavior of the function.
    enum class SomeEnum {
        GOOD,
        BAD
    }
    
    fun doTheEnumThing(someEnum: SomeEnum) {
                when (someEnum) {
                    SomeEnum.GOOD -> println("good")
                }
    }
    r
    p
    c
    • 4
    • 9
Powered by Linen
Title
s

Steven McLaughlin

10/22/2018, 5:47 PM
This is a noob question and may have been asked before, but what does the when block do by default if a case is not found? Like in the following example, what is actually happening when I pass
BAD
into that function? Essentially, I want to make the when block exhaustive but not change the behavior of the function.
enum class SomeEnum {
    GOOD,
    BAD
}

fun doTheEnumThing(someEnum: SomeEnum) {
            when (someEnum) {
                SomeEnum.GOOD -> println("good")
            }
}
r

Ruckus

10/22/2018, 5:52 PM
To answer your question, nothing. It works the same as
fun doTheEnumThing(someEnum: SomeEnum) {
    if (someEnum == SomeEnum.GOOD) {
        println("good")
    }
}
There's no else statement, so nothing happens. It just returns.
👍 2
s

Steven McLaughlin

10/22/2018, 5:53 PM
What I'm trying to figure out is like.. what would I put in the BAD case so that it has the same behavior? Just something like this?
when (someEnum) {
                SomeEnum.GOOD -> println("good")
                SomeEnum.BAD -> return
            }
r

Ruckus

10/22/2018, 5:55 PM
If there isn't a bad case, why do you want to add a stub for one? But if you do want it, you could do
SomeEnum.BAD -> Unit
// or
SomeEnum.BAD -> {}
👍 1
p

pavel

10/22/2018, 5:55 PM
that would return out of the function which might not be what you want
r

Ruckus

10/22/2018, 5:56 PM
@pavel I assume that was meant for @Steven McLaughlin's code block and not mine, correct?
p

pavel

10/22/2018, 5:56 PM
yes
👍 1
s

Steven McLaughlin

10/22/2018, 5:56 PM
Cool. I mostly want to stub one because the ide is giving me warnings haha
r

Ruckus

10/22/2018, 5:57 PM
You should probably also add a comment to make it clear it wasn't a mistake
SomeEnum.BAD -> Unit  // Do Nothing
// or
SomeEnum.BAD -> {}  // Do Nothing
👍 1
c

cedric

10/22/2018, 8:18 PM
Note that if your
when
is a value for an expression, the compiler will mandate exhaustiveness
View count: 3