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

    adam-mcneilly

    12/02/2018, 6:37 AM
    Is there a fancy collections helper method that can help me do this sort of nested for loop when I need to consider each item in a list with the others?
    for (firstIndex in (0..inputs.size - 2)) {
            for (secondIndex in (firstIndex + 1..inputs.size - 1)) {
                val firstInput = inputs[firstIndex]
                val secondInput = inputs[secondIndex]
                ...
            }
        }
    j
    r
    • 3
    • 4
  • g

    gabin

    12/02/2018, 12:26 PM
    What is the difference between coroutine context and coroutine scope ?
    a
    • 2
    • 1
  • f

    febs

    12/02/2018, 7:29 PM
    I just discovered that you can explicitly use the word "constructor" when defining a class; and that is mandatory only when specifying a visibility modifier for the constructor, or when using an annotation. Hence, I am curious about what annotation one could use for a constructor and why. Many thanks.
    s
    a
    • 3
    • 2
  • a

    Adam Hurwitz

    12/04/2018, 5:03 AM
    Hola Everyone, I'm looking for the best strategy to host a Kotlin app on AppEngine and have the main method run once deployed, similar to how a Jars main method runs. In the applications's main method is logic to retrieve content from an API request based on a Timer and save that information to a database. Jars take a long time to deploy to AppEngine (10 - 20 min) which slows down development and is why I would like to move away from this approach. I've deployed using Ktor, but the main method doesn't appear to be initialized without an endpoint being called. Any insight is appreciated! 🙂
    g
    • 2
    • 5
  • c

    CamilleBC

    12/05/2018, 10:32 AM
    ... Sorry I should have posted in Android.
    r
    • 2
    • 1
  • n

    Nicolas Pittion-Rossillon

    12/06/2018, 1:10 PM
    Hey guys! I was wondering about a replacement for DataScience tools such as Pandas DataFrames in Python. Would anyone be able to suggest some reading materials, links or whatever about that? Even opinions are welcome! I’m used to DataFrames and i would love trying to improve what i do with Kotlin.
    d
    • 2
    • 5
  • k

    karelpeeters

    12/07/2018, 12:49 AM
    .asSequence()
    👍 2
    o
    j
    • 3
    • 9
  • p

    poohbar

    12/07/2018, 2:15 AM
    any easy way to increment a value of a key in map? even if the mapping does not exist yet?
    s
    k
    • 3
    • 3
  • a

    Andreas Sinz

    12/07/2018, 4:26 PM
    @Steven McLaughlin what do you mean with obviously doesn't work?
    s
    s
    • 3
    • 3
  • f

    febs

    12/09/2018, 6:59 AM
    Is it correct to say that the parameters in the default constuctors are not properties unless you don't define them as such? I. E. class Cippa ( a: Int, val b: String) b is a property but a is not. Also, when could it be useful to define parameters that are not properties? Hope the question's clear. Thanks everyone!
    p
    a
    • 3
    • 5
  • s

    Slackbot

    12/09/2018, 3:31 PM
    This message was deleted.
    s
    b
    • 3
    • 2
  • f

    febs

    12/09/2018, 9:06 PM
    ...but why has that been removed? Looks useful to me.
    k
    o
    h
    • 4
    • 5
  • s

    Shan

    12/09/2018, 9:09 PM
    Hello all. I have a question I'm unsure how to google. Is it possible to have a function which takes as a parameter
    anything that extends x
    ? For example I have a data class here (using tornadofx library, it provides the ViewModel class) which I would like for the lamda to take anything which extends a ViewModel.
    data class CustomMenuItem (val name: String, val action: (E ? extends ViewModel) -> Unit)
    d
    • 2
    • 6
  • f

    febs

    12/10/2018, 7:34 AM
    I feel like this sentence from an article I'm reading is incorrect:
    With Java, in-memory data “storage” comes in four shapes: variables, parameters, fields, and constants, in order of scope.
    With Kotlin, parameters are similar with a few variations. They are final (so they cannot be assigned a new value), can have a default value (so we can omit > them when calling it) and their names matter (as we can use them when invoking).
    Now, for the other three, things are more different. As in, they are all defined as properties.
    I understood that properties are related to objects/classes (and they might have a backing field), but if I declare a val or var in a function then that is a variable Am I correct? Or wrong? Thank you 🙏
    k
    a
    • 3
    • 4
  • f

    febs

    12/10/2018, 8:19 AM
    why the allByDefault declaration is marked as an error while the declaration of simple is not? This is the official documentation.
    p
    • 2
    • 2
  • n

    nestserau

    12/10/2018, 10:25 AM
    Is there a way to declare this Swift enum using sealed classes in Kotlin?
    enum Optional<T> {
        case None
        case Some(T)
    }
    d
    o
    +3
    • 6
    • 11
  • i

    itnoles

    12/10/2018, 9:10 PM
    As for Electron, https://medium.com/@liran.tal/malicious-modules-what-you-need-to-know-when-installing-npm-packages-12b2f56d3685 very sad
    j
    • 2
    • 6
  • a

    Andreas Sinz

    12/10/2018, 9:18 PM
    @Robert you can try #tornadofx
    r
    r
    • 3
    • 3
  • r

    Robert

    12/10/2018, 9:24 PM
    I have been trying to get the Java logger to work, but it keeps saying the reference to LoggerFactory is unresolved. Should I replace it with something else, or is it my project setup?
    companion object { 
            private val log = LoggerFactory.getLogger(MyClass::class) // TODO
    s
    c
    m
    • 4
    • 9
  • r

    Robert

    12/10/2018, 10:49 PM
    One final question before I'm off to bed. My project doesn't seem to find any java related resources. For example when I try to import anything from the java namespace
    java.etc
    java turns red and is unresolved. I'm using gradle in IntelliJ IDEA CE. This is my multiplatform gradle file: https://gist.github.com/RdeWilde/eee5e77880971f77ee61518a5b447d7d Is there anything that i am missing?
    s
    • 2
    • 13
  • d

    dvlwj

    12/11/2018, 3:30 AM
    guys i just learning android mvp, but i need to have some
    context
    in the
    presenter
    , what i need to do? i need to make
    http request
    with the
    data
    in
    shared preference
    l
    • 2
    • 3
  • r

    Robert

    12/11/2018, 8:22 PM
    I am looking for a tutorial or something on a workflow to modify external library that I use in my project. So fork it, change it, test it, pull request. Can I just change it somehow in my main project? Or do I have to clone it in a seperate project per library? (Using IntelliJ IDEA CE)
    m
    • 2
    • 2
  • o

    Olivier

    12/12/2018, 8:59 AM
    MVI ?
    👍 2
    m
    d
    +3
    • 6
    • 16
  • d

    dvlwj

    12/13/2018, 2:23 AM
    btw did anyone have a legit MVVM tutorial/page/article/whatever for newbie? i am trying it, but i think i am missing something, and well, i doesn't found one that is legit for newbie, most of them using RXthis RXthat, Dagger, etc which is i never use before. 😞
    👍 1
    h
    j
    m
    • 4
    • 21
  • o

    oday

    12/13/2018, 4:40 PM
    this is how it turned out, but now I never get any results, although in the debugger the program is reaching the correct filter based on the correct toggle and knows when a toggle is active or not and oly checks against the appropriate predicate https://pastebin.com/mPuWzSGQ
    j
    • 2
    • 3
  • o

    oday

    12/13/2018, 5:44 PM
    the idea is there though @karelpeeters?
    k
    a
    • 3
    • 64
  • m

    Mike

    12/14/2018, 12:03 PM
    And really, Java isn't oo in the way Alan Kay et al meant it to be. They meant oo more the way actors work. Objects that work independently and communicate via messages. And now I'm totally off topic...
    d
    • 2
    • 2
  • b

    blogscot

    12/14/2018, 3:07 PM
    Is there an nice way to generate a sequence like 0 1 2 3 2 1 0 …?
    a
    a
    +3
    • 6
    • 11
  • g

    Giulio

    12/15/2018, 5:50 PM
    so this channel is really useless
    k
    s
    +3
    • 6
    • 68
  • p

    poohbar

    12/16/2018, 3:26 PM
    ran into something a bit weird and was wondering if anyone could clear it up for me:
    list.map {
      if (condition) {
        42
      }
      0 
    }
    I am used to not doing else branches in Java when not needed but it seems like here I get a list of zeros if I don't put the 0 into an else branch. 🤔 Has this bitten anyone before?
    b
    d
    +4
    • 7
    • 14
Powered by Linen
Title
p

poohbar

12/16/2018, 3:26 PM
ran into something a bit weird and was wondering if anyone could clear it up for me:
list.map {
  if (condition) {
    42
  }
  0 
}
I am used to not doing else branches in Java when not needed but it seems like here I get a list of zeros if I don't put the 0 into an else branch. 🤔 Has this bitten anyone before?
b

blogscot

12/16/2018, 3:35 PM
The last line within the map lambda is what you’re returning as the result. So your example will always result a list of zeros. `if`s in kotlin are expressions; in this example your throwing away the result.
d

Dominaezzz

12/16/2018, 3:36 PM
I struggled with this for days. Only the very last expression in the block is returned. So, with the else branch, the if expression is the last expression but without the else branch, 0 is the last expression.
p

poohbar

12/16/2018, 3:38 PM
i see.. thanks!
b

blogscot

12/16/2018, 3:38 PM
I tweaked your example @poohbar:
fun main() {
  val list = listOf(1,2,3,4,5)

  val result = list.map {
    if (it and 1 == 0) {
      42
    } else {
      0
    }
  }
  println(result)
}
Now it returns
[0, 42, 0, 42, 0]
p

poohbar

12/16/2018, 3:39 PM
yeah i figured it out, was just wondering about "why"
b

blogscot

12/16/2018, 3:43 PM
It’s a common practice in FP languages to use the last expression of a function as the return value. Saves typing, I guess
b

bjonnh

12/16/2018, 4:18 PM
almost any function has a return
so if you do:
if (condition) { log.somethingabout(it) } 0 log.somethingabout would return
which is not what you want here
j

Jukka Siivonen

12/16/2018, 7:00 PM
Yeah I was bitten by this, would be nice to get some warning at least. Also you can't put explicit return there so only way is to remember put else there
a

AngusMorton

12/17/2018, 1:09 AM
You can do a labelled return e.g.
return@map
(https://kotlinlang.org/docs/reference/returns.html#return-at-labels)
b

bjonnh

12/17/2018, 3:52 AM
Totally forgot about those. Thanks.
h

Hauke Radtki

12/17/2018, 9:12 AM
Watch the Kotlin Puzzlers from KotlinConf '17 & '18, helps a lot wrapping your head around the weirdnes that kotlin can offer 😉
View count: 1