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/01/2017, 5:21 PM
    Also related to AoC earlier - does Kotlin have any circular linked list data type or would I just use any Java implementation that I've previously had? My instincts tell me no since most Kotlin collections are based upon the Java ones.
    k
    s
    • 3
    • 14
  • d

    dierre

    12/02/2017, 5:55 PM
    mmm..how do I access to the next element using fold? the operation would be: if next is bigger than the current value
    o
    • 2
    • 1
  • d

    Diaa Jad

    12/02/2017, 11:46 PM
    I might be overlooking an obvious solution, but how to convert a
    char
    to an
    Int
    ?
    var.toInt()
    returns the ASCII value of the character.
    s
    a
    a
    • 4
    • 6
  • s

    Shawn

    12/05/2017, 7:51 PM
    if you want the resulting type to be
    Map<String, List<String>>
    you’re probably going to have to do some casting
    b
    • 2
    • 4
  • k

    katien

    12/05/2017, 8:12 PM
    thank you guys for explaining all that 🙂 managed to put it all together and get it working with mapValues
    errors.mapValues { (_, v) ->
                         when (v) {
                            is String -> listOf(v)
                            is List<*> -> v as List<String>
                            else -> listOf(v.toString())
                        }
                    }
    👍 1
    r
    s
    • 3
    • 3
  • r

    Regan Russell

    12/05/2017, 9:33 PM
    I want to do something like this: mylist.sort { it.popular or not } and then mylist.sort { it.name ascending } in the one list. I want the popular at the top and the rest sorted by name
    s
    a
    i
    • 4
    • 7
  • s

    spragg

    12/06/2017, 2:45 AM
    This blog might help https://proandroiddev.com/understanding-generics-and-variance-in-kotlin-714c14564c47
    👍 1
    k
    a
    • 3
    • 31
  • p

    PN10

    12/06/2017, 6:47 AM
    Hi folks, Suggest me MOOC for kotlin looking for some really good beginner stuff to start with kotlin for Android??Free stuff only??
    k
    j
    • 3
    • 7
  • j

    Jakub Kornatowski

    12/06/2017, 1:26 PM
    Hi all guys! Looking for good tutorial for Kotlin + Spring Boot (Notice that I don’t know Spring)
    a
    • 2
    • 1
  • k

    karelpeeters

    12/07/2017, 5:33 PM
    thingy?.let { Foo(it) } ?: Foo(defaultThingy, additionalParam)
    i
    h
    • 3
    • 3
  • k

    kastork

    12/07/2017, 8:53 PM
    From the guide I know that
    lateinit
    vars cannot be primitive types. If you're trying to create a bean-compliant class, how do you designate an
    Integer
    or
    Double
    type? If you make the property type
    Int
    you get the error that primitive types can't be
    lateinit
    , if you give it the type
    Integer
    you get the warning that you shouldn't use
    Integers
    but should use
    Int
    .. And for
    Double
    there doesn't even appear to be a way to say you want the Object
    Double
    and not a primitive
    double
    r
    k
    • 3
    • 10
  • s

    shamrock_frost

    12/08/2017, 12:10 AM
    What's the most idiomatic way to match on the type of multiple expressions? e.g.
    class A
    class B : A
    class C : A
    
    class X
    class Y : X
    class Z : X
    
    fun example(a : A, x : X) = when(a, x) {
      is B, is Y -> "B and Y"
      is C, is Z -> "C and Z"
      else -> "Not an interesting case"
    }
    u
    • 2
    • 3
  • a

    adam-mcneilly

    12/08/2017, 4:55 PM
    God I knew I would be mad I remember it being an obvious name. Thank you.
    s
    • 2
    • 3
  • d

    dierre

    12/09/2017, 10:21 AM
    Hi, I'm experimenting with kotlin multiproject. There is something that it's not clear to me. My goal is to port a node js project, a simple one ( https://github.com/invasionofsmallcubes/deals-bot ). The idea is to begin with the porting, isolate common code and repository interfaces on the common project, and then create the jvm version using
    TelegramBots
    (https://github.com/rubenlagus/TelegramBots). The goal is to have two running bots, the js and the kotlin one with one common business logic. I hope it's all clear up to this point. Now the question: for testing, on the node js I used jest + nock. Now I would see, as an advantage, the fact that I can use a java/kotlin mock library (I want to mock the telegram API) inside kotlin test. Of course the library that I will use to implement the bot is a javascript library. So, basically, can I implement my telegram bot in kotlin, using telegram bot js library, and testing it with, as an example, OkHttp MockWebServer?
    c
    • 2
    • 1
  • t

    techie01

    12/11/2017, 11:49 AM
    how to get the time in this format datetime.datetime(2015, 12, 5, 18, 17, 58, tzinfo=<UTC>)
    a
    • 2
    • 1
  • d

    danneu

    12/11/2017, 9:51 PM
    i mean what's a nice way for others to actually download it? perhaps github release zipballs
    a
    • 2
    • 1
  • d

    danneu

    12/12/2017, 1:32 AM
    that's the problem with an api like
    ctx.write(Any)
    e.g.
    ctx.write(response)
    in this case. type system can't help
    a
    • 2
    • 4
  • k

    karelpeeters

    12/12/2017, 9:04 AM
    Kotlin doesn't have a ternary operator, use
    if(...) then ... else ...
    .
    😞 2
    k
    • 2
    • 7
  • e

    elye

    12/13/2017, 12:58 AM
    Could someone help check out this https://stackoverflow.com/questions/47783929/kotlin-nullibility-check-if-else-functional-approach-how. Thank you!!!
    d
    u
    • 3
    • 2
  • g

    GauthierPLM

    12/13/2017, 1:34 PM
    Hi, how can I take elements between two index in a collection? For example, I have a list of 5 elements and I want to take the second to the fourth elements of this list.
    m
    k
    d
    • 4
    • 11
  • m

    melston

    12/13/2017, 10:48 PM
    I found Kotlin in Action (Manning) to be pretty good.
    ❤️ 3
    l
    • 2
    • 1
  • l

    lawlorslaw

    12/14/2017, 6:34 PM
    What is the recommended approach to a first Kotlin project? Start writing Kotlin code in an Android project, or convert an existing Android project written in Java to Kotlin?
    o
    s
    • 3
    • 10
  • d

    danneu

    12/15/2017, 4:45 AM
    what's a good way to quickly bench competing implementations of a function? is there something robust but as simple as running each function in a loop + system.getCurrentMillis(), and then doing it a few times to warm up the jvm?
    n
    a
    • 3
    • 2
  • t

    techie01

    12/15/2017, 10:53 AM
    i am from hyderabad can i get the kotlin hard copy book
    o
    j
    • 3
    • 2
  • a

    asad.awadia

    12/16/2017, 1:39 AM
    Isnt it imported by default with the kotlin.collections package?
    a
    a
    • 3
    • 8
  • t

    Tachyon

    12/17/2017, 1:04 PM
    well, I converted the whole project to kotlin
    a
    • 2
    • 1
  • v

    visakha

    12/17/2017, 1:44 PM
    i.e (abc){lambda} rather than (abc,{lambda})
    a
    • 2
    • 1
  • r

    rashadfl

    12/17/2017, 7:14 PM
    Just started this course and I'm uber new programming. Things were going well for me until section which deals with the terminal after stumbling my way through this section I finally have ran into a road block I can't over come these two issues... Can't start Git: C:\Program Files (x86)\Git\git-cmd.exe Probably the path to Git executable is not valid and this Invalid VCS root mappings The following directories are registered as VCS roots, but they are not: <Project> C:\Users....\Desktop\Test
    🚓 3
    k
    o
    • 3
    • 5
  • d

    dgngulcan

    12/17/2017, 9:24 PM
    I am to write an SDK and to decide to write with Kotlin or Java. Is there any comparison articles or some sort of guide about best practices on writing APIs in Kotlin that can be called from Java without ugliness? Thanks
    r
    • 2
    • 2
  • k

    KronicDeth

    12/18/2017, 3:30 PM
    Can I turn this into a property with a
    private set
    and custom
    get
    ?
    java
      private ProjctSdksModel myModel
      public ProjectSdksModel getModel() {
        if (myModel == null) {
          myModel = new ProjectSdksModel();
          myModel.reset(null);
        }
        return myModel;
    If I try
    kotlin
    var model: ProjectSdksModel? = null
            get() {
                if (this.model == null) {
    
                }
    
                return this.model
            }
            private set
    I get warning that
    this.model
    is or
    model
    is recursive and my google-fu is failing for how to refer to the current property inside the getter
    r
    u
    • 3
    • 9
Powered by Linen
Title
k

KronicDeth

12/18/2017, 3:30 PM
Can I turn this into a property with a
private set
and custom
get
?
java
  private ProjctSdksModel myModel
  public ProjectSdksModel getModel() {
    if (myModel == null) {
      myModel = new ProjectSdksModel();
      myModel.reset(null);
    }
    return myModel;
If I try
kotlin
var model: ProjectSdksModel? = null
        get() {
            if (this.model == null) {

            }

            return this.model
        }
        private set
I get warning that
this.model
is or
model
is recursive and my google-fu is failing for how to refer to the current property inside the getter
r

Ruckus

12/18/2017, 3:35 PM
Inside your getter, you need to replace
this.model
with
field
https://kotlinlang.org/docs/reference/properties.html#backing-fields
k

KronicDeth

12/18/2017, 3:35 PM
Thanks @Ruckus
👍 1
Is there something I should be doing different to avoid having to do
!!
immediately after initializing the
field
? I assume the mutability warning is for concurrency
get() {
            if (field == null) {
                field = ProjectSdksModel()
                field!!.reset(null)
            }

            return field
        }
Maybe I should go the "Backing Properties" route, because the getter is non-null even if the field is null
r

Ruckus

12/18/2017, 3:39 PM
You could always use something like
field = ProjectSdksModel().apply { reset(null) }
But in this case, backing properties are the way to go.
k

KronicDeth

12/18/2017, 3:41 PM
Is there a more compact way to handle the
null
reset back to
field
. It's not just a lateinit - there's other code that can reset the field to
null
r

Ruckus

12/18/2017, 3:43 PM
I don't know what
reset(null)
does, but a backing property should help separate the logic more cleanly.
u

umar

12/18/2017, 4:32 PM
You can use lateinit backing property
View count: 2