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

    gabin

    11/21/2018, 11:51 AM
    The_snippet_that_causes_crash.kt
    t
    i
    • 3
    • 5
  • o

    oday

    11/21/2018, 2:20 PM
    shouldn’t the
    else
    of a
    when
    include all possible values of the variable that are not mentioned in the when?
    d
    • 2
    • 1
  • o

    oday

    11/21/2018, 2:20 PM
    like, all else, falls into that block?
    m
    • 2
    • 1
  • a

    ageery

    11/21/2018, 9:41 PM
    I want to write an extension method for a list that returns a new list with any null items replaced by a default value. I'm having trouble with the generics. There is one generic for the list and there is another generic for the replacement item. How can I specify this relationship between the two generics? I think this is close:
    fun <C: Any, T: C?> List<T>.nonNull(sub: C) = map { it ?: sub }
    . However, the compiler can't infer the type of C if there is a null in the list. For example, this doesn't compile:
    val x = listOf("x", null).nonNull("y")
    . You have to explicitly specify the types:
    val x = listOf("x", null).nonNull<String, String?>("y")
    . Is there something wrong with my method signature that prevents the compiler from inferring the types?
    s
    a
    • 3
    • 16
  • h

    henrik

    11/22/2018, 8:56 AM
    fun toMap(): Map<String, Double> = mapOf("key" to nullableDouble1, "key2" to nullableDouble2).filterValues { it != null }
    complains that "expected
    Map<String, Double>
    but found `Map<String, Double?>`". Is there an easy way around this other than appending
    as Map<String, Double>
    ?
    s
    k
    • 3
    • 20
  • s

    Sartori

    11/22/2018, 4:44 PM
    Hi guys, anyone know if is possible access that variable like this? I try to see the auto complete but the variable does not appear 😞
    -.kt
    🙅‍♂️ 1
    m
    • 2
    • 1
  • j

    jojo.lichtenberger

    11/24/2018, 1:50 PM
    It's from Vert.x with their Kotlin API
    k
    • 2
    • 2
  • d

    dewildte

    11/24/2018, 7:57 PM
    let {}
    is kinda cryptic.
    s
    • 2
    • 13
  • j

    jschneider

    11/24/2018, 7:59 PM
    Why is there not "it" in when?
    s
    d
    • 3
    • 13
  • l

    liminal

    11/25/2018, 6:27 PM
    what are some good examples of
    takeIf {}
    s
    e
    • 3
    • 3
  • e

    elye

    11/26/2018, 6:37 AM
    Hi What’s the different between the two below?
    (origin as? Implementor)?.someFunction()
    vs
    (origin as Implementor?)?.someFunction()
    r
    • 2
    • 2
  • f

    febs

    11/26/2018, 12:50 PM
    Normally you can't declare a var without assigning it a value; unless you state that it is a "lateinit" var. You can't declare a val withoutu assigning it a value too; but you can use "by lazy" to achieve that. BUT THEN Why in a { catch (e: Exception) } block I am allowed to define a val or a var and then assign it afterwards, without the need of "lateinit" nor "by lazy"?
    d
    • 2
    • 13
  • d

    dG

    11/26/2018, 3:36 PM
    foo(first : SomeObj = validSomeObj()) : Bar = foo(first.toString())
    foo(first : String = validSomeObj().toString())  : Bar = Bar(first)
    looks like this
    o
    • 2
    • 1
  • s

    serebit

    11/26/2018, 6:07 PM
    @dG I’ve seen that as well. Making one of the functions into a block body fixes it, for some reason.
    a
    d
    • 3
    • 3
  • q

    Quy D X Nguyen

    11/27/2018, 4:18 AM
    What's the right way to deserialize JSon into Kotlin data classes, accounting for nonnullability?
    e
    • 2
    • 1
  • e

    elect

    11/27/2018, 8:46 AM
    the whole
    buildscript
    section?
    n
    s
    • 3
    • 5
  • l

    liminal

    11/28/2018, 3:39 AM
    how would i check if a float number has any non-zero decimal places 1f vs 1.2f
    h
    n
    • 3
    • 6
  • c

    claudiug

    11/28/2018, 11:21 AM
    generate class is MainKt
    k
    a
    • 3
    • 43
  • s

    Slackbot

    11/28/2018, 11:31 AM
    This message was deleted.
    k
    • 2
    • 1
  • n

    Narayan Iyer

    11/28/2018, 4:44 PM
    So, what's the problem here? I am used to head/tail in Scala, so I am writing my own head/tail:
    fun <T> head(ls: List<T>): List<T> = ls.get(0)
    error: type mismatch: inferred type is T but List<T> was expected
    fun <T> head(ls: List<T>): List<T> = ls.get(0)
                                         ^
    m
    • 2
    • 2
  • s

    stephan_marshay

    11/28/2018, 4:49 PM
    val<T> List<T>.head: T 
       get()= this[0]
    👍 2
    n
    h
    • 3
    • 5
  • n

    Narayan Iyer

    11/28/2018, 4:49 PM
    So, when I write my tail function like so:
    fun <T> tail(ls: List<T>): List<T> {
       return ls.drop(1)
    }
    why is it insisting on the
    return
    statement?!
    c
    s
    • 3
    • 9
  • s

    stephan_marshay

    11/28/2018, 4:50 PM
    because you're declaring a return type (
    List<T>
    )
    m
    • 2
    • 5
  • h

    hudsonb

    11/28/2018, 5:18 PM
    Is
    run
    needed there?
    s
    • 2
    • 3
  • n

    nekomatic

    11/29/2018, 10:44 AM
    Hi All, does any of you know of an elegant way to check for functions equality in Kotlin? I’m looking for a way to check if a function created with certain set of captured values can be verified as equal to same function created with a different but structurally equal set of captured values. Any Ideas?
    a
    • 2
    • 2
  • t

    tipsy

    11/29/2018, 6:19 PM
    currently i have
    Pair(str.split(Regex(" "), 2)[0], str.split(Regex(" "), 2)[1])
    , i could also use indeces.. is there a more elegant way?
    d
    s
    s
    • 4
    • 10
  • d

    dMusicb

    11/29/2018, 6:40 PM
    Does Kotlin have a way of letting you do something like:
    for (i in (1..10)) {
        // do stuff
        someFunc() // inside someFunc call continue to skip below part
        // do other stuff
    }
    r
    c
    • 3
    • 2
  • u

    ursus

    11/30/2018, 2:06 AM
    what I meant is wether is there a way to alias it to void, i.e. no return value when that kotlin function is used in java
    k
    • 2
    • 3
  • s

    Slackbot

    11/30/2018, 12:26 PM
    This message was deleted.
    o
    • 2
    • 1
  • f

    febs

    12/01/2018, 6:32 PM
    About function literals. Both lambdas and anonymous functions are function literals according to the documentation https://kotlinlang.org/docs/reference/lambdas.html#instantiating-a-function-type I am wondering when anonymous functions should be used. I have seen plenty of uses of lambdas in code samples and tutorials but I still have to find some usage for an anonymous function. Where should I use one in place of a lambda, or simply, where should I just use one? Thanks ❤️
    h
    k
    • 3
    • 2
Powered by Linen
Title
f

febs

12/01/2018, 6:32 PM
About function literals. Both lambdas and anonymous functions are function literals according to the documentation https://kotlinlang.org/docs/reference/lambdas.html#instantiating-a-function-type I am wondering when anonymous functions should be used. I have seen plenty of uses of lambdas in code samples and tutorials but I still have to find some usage for an anonymous function. Where should I use one in place of a lambda, or simply, where should I just use one? Thanks ❤️
h

hudsonb

12/01/2018, 6:45 PM
I think the only difference is in the
return
behavior
✔️ 1
k

karelpeeters

12/02/2018, 12:16 PM
I personally haven't found a use case for anonymous functions yet.
✔️ 1
View count: 4