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

    bbaldino

    11/06/2019, 8:18 PM
    is there a way to convert a KFunction to a lambda other than just casting it?
    r
    k
    +2
    • 5
    • 11
  • k

    Konstantin Petrukhnov

    11/07/2019, 9:33 AM
    I'm looking at the code that I wrote before. There is cache:
    val cache = MutableMap<String, Any?>
    where value could be String, Int, Map, Array, etc. Then i retrieve values from it through the chain of functions, where caller is responsible to specify correct type.
    cache[key] as R?
    Question: what problems I may get with that approach? All of this code is internal to the module, and run on jvm/ios.
    k
    m
    • 3
    • 5
  • o

    oday

    11/07/2019, 10:28 AM
    im really unsure why this is happening
    j
    w
    • 3
    • 17
  • k

    Konstantin Petrukhnov

    11/07/2019, 2:02 PM
    If I have
    val myvalue: MyObj by lazy {...}
    , how I could inject different object to myvalue during testing?
    c
    d
    • 3
    • 3
  • k

    Konstantin Petrukhnov

    11/08/2019, 12:03 PM
    What is the fastest way to check that 2 collections (lists with possible duplicates) have same content? 1. two containsAll calls 2. intersect and check size to each set 3. union and check size to each set 4. union size same as intersect size 5. anything else?
    m
    s
    +4
    • 7
    • 18
  • g

    gregorbg

    11/08/2019, 3:40 PM
    Hey! Say I have an arbitrary
    List<Int>
    where every entry is a counter. What would be a Kotlin-esque way to map this into a
    List<Int>
    where at every index the entry is equal to the sum of counts until that index? For example,
    [1, 3, 4, 2, 2]
    becomes
    [1, 4, 8, 10, 12]
    due to the following summations:
    [1, 1+3, 1+3+4, 1+3+4+2, 1+3+4+2+2]
    c
    • 2
    • 2
  • p

    Pallavi

    11/10/2019, 3:48 PM
    When would one prefer way 1 over way 2 or vice-versa for writing function
    doSomething
    ?
    //Way 1 (doSomething is a member function)
    data class A(val item: String) {
            fun doSomething(): String{
                return item.toUpperCase()
            }
    }
    
    //Way 2 (making doSomething an extension function)
    data class A(val item: String)
    fun A.doSomething(): String {
            return item.toUpperCase()
     }
    ➕ 1
    m
    a
    +2
    • 5
    • 12
  • f

    Frodrigues

    11/10/2019, 5:32 PM
    How I create a class with two "constructors" that on does not receive any value so run a big code block to init the properties and another that receives the properties already initialized?
    m
    m
    • 3
    • 4
  • t

    Terje Andersen

    11/11/2019, 8:25 AM
    Hi! 👋 I am trying to "flatten" a
    List<Map<String, List<T>>>
    to
    Map<String, List<T>>
    . If the original list of maps contain two maps containing the same String key, the lists should be merged / concatenated. I finding it hard to achieve this, could anyone nudge me in the right direction?
    a
    m
    • 3
    • 3
  • d

    dG

    11/11/2019, 10:30 AM
    if (someArray[x] != null) { someArray[x].add(foo) }
    Why does the compiler not recognize that
    someArray[x]
    cannot be null, and insists on
    !!
    ?
    k
    m
    +2
    • 5
    • 10
  • v

    Vishnu Haridas

    11/11/2019, 1:34 PM
    Hey everyone, which server framework will you recommend based on your experience? Looking for minimal run-time dependencies, or even run without a server container like Jetty.
    c
    r
    +2
    • 5
    • 17
  • a

    atsushi-koshikizawa

    11/12/2019, 6:13 AM
    I want to extend a data class.
    data class Hoge(val foo: String, val bar: String)
    
    data class Fuga(foo: String, bar: String, val buz: String) : Hoge(foo, bar) // compile error
    Data class in Kotlin cannot be opened, and its primary constructor must have only property parameters. I have two questions. 1. Why cannot data classes be opened? 2. How can I reuse
    Hoge
    class to define
    Fuga
    class?
    m
    • 2
    • 2
  • m

    Mr.NiceGuy

    11/12/2019, 8:11 AM
    Hi everyone, I am working with a local jfrog artifactory and I made build.gradle.kts file and followed how to configure it to integrate with the local repo, now I heared that the shadow plugin is good for making fat jars also in kotlin so I decided to use it, I wrote the following line inside my dependencies block: compile("shadow😒hadow:1.2.3") and it did pulled it from the local repo (can see the jar inside "external libraries" in intellij) but when I am trying to include the shadow plugin the build failed because it cannot find the shadow plugin which is very strange. Anyone have any idea how to add the shadow plugin properly?
    m
    • 2
    • 5
  • e

    Esa

    11/12/2019, 8:24 AM
    Hello, I am modeling the reading of a
    .csv
    -file, and want to be as explicit as possible. The file comes in a strict format, and I am able to say for sure what content-type each column will have. For this reason, I want to model it explicitly, like so:
    sealed class CsvField {
        data class ExternalId(val value: String) {
            companion object {
                const val index: Int = 0
            }
        }
    }
    So obviously there will be many more columns, which means the sealed class ends up looking extremely verbose when every data class has its own companion object, and there are like 15 of them. So back to my question; when my intent is to have 1 subclass of a sealed class with 1 constant, unchangeable field and one specified in constructor, is this the best way? Or am I missing something obvious? Is there a better pattern here? I tried with
    enum class
    first, but that way I don’t get to specify the value of the field, only the expected data. I could have a data class with the enum as type, and a plain string as value, but idk. Thoughts?
    k
    m
    • 3
    • 17
  • o

    Oleh Ponomarenko

    11/12/2019, 11:00 AM
    How can I do a loop like this but in Kotlin way, maybe using repeat or some another feature?
    var isStopped =false
    
            do {
    
            }while (isStopped.not())
    k
    • 2
    • 14
  • s

    Shumilin Alexandr

    11/13/2019, 7:26 AM
    Hi everyone. I am java developer. Tell me please. How can i start learn kotlin? What the training exsercise or examples? I want starting learn Kotlin for back end development. I want to learn English the same way)
    m
    • 2
    • 3
  • f

    Florian

    11/13/2019, 8:43 AM
    String.kt for example will use String.java on the JVM?
    d
    • 2
    • 10
  • j

    Joey

    11/13/2019, 9:07 AM
    Hi anyone knows how to publish kotlin shared library in which can be used by iOS and Android? Thanks!
    m
    • 2
    • 1
  • t

    Tuang

    11/13/2019, 1:38 PM
    hi Is there any useful or popular web framework for kotlin. Something likes cakePHP or …..
    m
    m
    • 3
    • 6
  • s

    Shumilin Alexandr

    11/13/2019, 2:51 PM
    Hi 2 all! please explain to me, what's difference? advantage?
    Untitled
    m
    m
    • 3
    • 5
  • s

    Shizo

    11/13/2019, 10:52 PM
    Hi, could you help me spot the bug? The code here works, but the test fails with a message about invalid json. When i test it with postman and intellij idea's http file everything works. The error message:
    Cannot transform this request's content to org.user5145.account.domain.aggregate.Account
    https://pastebin.com/LZmcMx1u PS. it uses ktor
    t
    • 2
    • 2
  • m

    magnumrocha

    11/14/2019, 8:38 AM
    however, you can access Firebase via the REST API (https://firebase.google.com/docs/firestore/use-rest-api), and make the access by a Http Client like ktor
    j
    • 2
    • 20
  • d

    df

    11/14/2019, 10:22 PM
    i'm sure there is a pretty way to express sth. like this in kotlin?
    return if (sortedItems.isEmpty()) null else itemMapper.mapDto(sortedItems.first())
    m
    • 2
    • 2
  • e

    Esakki

    11/15/2019, 11:47 AM
    Hi all i am looking for a open source project to contribute please let me know if anyone have one
    i
    t
    r
    • 4
    • 4
  • j

    Jur

    11/15/2019, 8:39 PM
    Hi, A few years a go i developed a game(currently still active with 1M+ downloads) on Android in java. After that project we moved on the Unity.. I want to get back into native Android but a lot has changed. Where do you guys recommend to start with? Im creating a simple test app to learn Kotlin and all the new patterns. But is MVP still a thing or should i go with MVVP and live data?
    r
    a
    • 3
    • 6
  • m

    Mr.NiceGuy

    11/17/2019, 5:31 PM
    Anyone have any idea why I can't run a simple main function inside a class? Only if it's a standalone function I can run it but I can't run it if it's inside a class (even if it's given the jvmStatic annotation)
    m
    s
    e
    • 4
    • 13
  • j

    Joey

    11/18/2019, 6:46 AM
    Anyone know how to solve this? Cannot access 'Serializable': it is internal in 'kotlin.io'
    d
    • 2
    • 1
  • j

    jubril

    11/18/2019, 9:11 AM
    Hello guys, what's a fast way to get started with Kotlin backend development?
    h
    e
    • 3
    • 3
  • j

    Joey

    11/18/2019, 9:29 AM
    Anyone know how to fix this? This declaration is experimental and its usage must be marked with '@io.ktor.util.InternalAPI' or '@UseExperimental(io.ktor.util.InternalAPI::class)'
    t
    • 2
    • 2
  • e

    Ellen Spertus

    11/18/2019, 7:32 PM
    If I’m using
    when
    as an expression, can there be multiple statements in an arm?
    d
    a
    h
    • 4
    • 10
Powered by Linen
Title
e

Ellen Spertus

11/18/2019, 7:32 PM
If I’m using
when
as an expression, can there be multiple statements in an arm?
d

Daniel Rampelt

11/18/2019, 7:34 PM
Yes, you can use
-> { ... }
and put multiple lines and statements between the
{}
e

Ellen Spertus

11/18/2019, 7:35 PM
Thanks. How do I indicate the value of the arm? Does it just sit alone at the end? @Daniel Rampelt
d

Daniel Rampelt

11/18/2019, 7:35 PM
For example
val result = when (x) {
  condition -> {
    statement
    another statement
    last statement is the value
  }
}
✔️ 1
💯 2
e

Ellen Spertus

11/18/2019, 7:36 PM
Got it. Thanks!
👍 1
d

Daniel Rampelt

11/18/2019, 7:36 PM
yep the last statement in the block is the value
a

Andy Gibel

11/18/2019, 7:52 PM
I always find this pretty unreadable. I try to make private functions unless it really doesn't make sense
👍 1
h

Hullaballoonatic

11/18/2019, 11:17 PM
I find it plenty readable enough if you format all arrows to be at the same indentation and potentially put an extra blank like after block branches
extra points if the only block branch is the final one
@Ellen Spertus last line is the return value in
if/else
and scoped functions (
let
,
run
, etc...) as well
e

Ellen Spertus

11/18/2019, 11:21 PM
@Hullaballoonatic, got it, like in Scheme sequences.
View count: 4