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

    oday

    01/16/2018, 9:29 AM
    im just going to replace , with “”
    d
    5 replies · 2 participants
  • s

    Sudhir Singh Khanger

    01/16/2018, 11:30 AM
    How does multiple assignment work is Kotlin?
    int mBaseSystemUiVisibility = SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | SYSTEM_UI_FLAG_LAYOUT_STABLE;
    t
    c
    3 replies · 3 participants
  • r

    rook

    01/16/2018, 5:41 PM
    UserModel.Creator(
    _, foo, bar, cat, moo, _ -> User(foo, bar, cat, moo)
    )
    works fine, but
    UserModel.Creator(
    _, foo, bar, cat, moo, _ -> {
    Log.d("")
    User(foo, bar, cat, moo)
    }
    )
    doesn’t work.
    m
    r
    +1
    8 replies · 4 participants
  • p

    patrickdelconte

    01/17/2018, 12:58 PM
    How do I "enable multiplatform projects explicitly"? I get the error message "Kotlin: The feature "multi platform projects" is experimental and should be enabled explicitly" on every expected/actual declaration in my project. The project works perfectly fine on my other computer, where I created the project with the IntelliJ wizard. On my new computer I get this error message when I build in IntelliJ, however gradle over CLI builds without errors
    i
    1 reply · 2 participants
  • o

    oday

    01/17/2018, 1:46 PM
    how can I turn two arrays into a map whose keys are from one array and values are from the other?
    m
    11 replies · 2 participants
  • d

    diesieben07

    01/17/2018, 2:15 PM
    No, the property could change, because it is mutable. Store it in a local variable, then do the nullcheck and access with that local variable.
    ➕ 2
    o
    1 reply · 2 participants
  • k

    kevinmost

    01/17/2018, 5:27 PM
    you can't recursively call an inline function. How would it inline the recursive calls?
    👍 3
    ➕ 1
    c
    1 reply · 2 participants
  • c

    ClaudiuB

    01/17/2018, 5:31 PM
    Oh I see, thanks for the input, it just needs a level of un-inlining, right?
    k
    2 replies · 2 participants
  • c

    ClaudiuB

    01/17/2018, 7:06 PM
    so use
    x as? y != null
    a
    m
    4 replies · 3 participants
  • w

    wck

    01/18/2018, 10:12 PM
    ah ok, thanks, was confused because
    job.isCompleted
    returns true after
    cancel()
    e
    1 reply · 2 participants
  • s

    siladu

    01/20/2018, 2:07 PM
    It's a prepend list which is more idiomatic for functional programming, e.g. separating head and tail in recursive functions: https://alvinalexander.com/scala/how-to-use-lists-nil-cons-scala-match-case-expressions
    k
    7 replies · 2 participants
  • f

    fabricio

    01/20/2018, 2:09 PM
    Intellij
    😀 1
    ☝️ 2
    z
    1 reply · 2 participants
  • i

    ianbrandt

    01/20/2018, 10:32 PM
    I'm getting a type mismatch error trying to spread an array of Ints. Am I'm doing something wrong, or is this a type inference limitation or bug?
    fun main(args: Array<String>) {
    	val ints = arrayOf(1, 2, 3)
    	// Type mismatch: inferred type is Array<Int> but IntArray was expected?
    	spreadTest(*ints)
    }
    
    fun spreadTest(vararg ints: Int) {
    	for (int in ints) {
    		println(int)
    	}
    }
    p
    3 replies · 2 participants
  • m

    Moemen

    01/21/2018, 1:49 PM
    I need to try a change in the local copy of Kotlin compiler source code. So I need to debug the new change. But I see it is not reflected as it is still using the installed version of compiler not the changed version. Can you please advice how to achieve that?
    i
    4 replies · 2 participants
  • m

    menegatti

    01/23/2018, 1:00 PM
    what’s the error message if you don’t change to
    Any
    ?
    b
    e
    8 replies · 3 participants
  • d

    dalexander

    01/23/2018, 7:15 PM
    What’s the filter doing? There might be a faster way to do it.
    j
    2 replies · 2 participants
  • s

    Shawn

    01/23/2018, 8:04 PM
    not sure what kind of api you’re working with, but if you can’t expect a given object to be shaped the same way each time you request it, I feel like you have bigger problems
    j
    c
    4 replies · 3 participants
  • s

    Shawn

    01/25/2018, 5:56 AM
    what part are you stuck on specifically?
    c
    1 reply · 2 participants
  • j

    jstuyts-squins

    01/25/2018, 12:32 PM
    You can also create an array directly:
    Array((2018 - 1960 + 1), { i -> (i + 1960).toString() })
    d
    e
    6 replies · 3 participants
  • m

    magicleon

    01/25/2018, 6:40 PM
    I’m new to this community, I wanted to learn Kotlin, especially focusing on Android Development. Where can I start?
    a
    s
    +1
    4 replies · 4 participants
  • q

    Qverkk

    01/25/2018, 8:06 PM
    anyone's got an android project in kotlin with mutli activities? like switching them on click or something and has a source on github?
    a
    3 replies · 2 participants
  • c

    chb0kotlin

    01/25/2018, 9:41 PM
    It's clear as hell in Java 🙂 - This is a porting excercise - rethinking the code is out-of-scope. Let me hack on this a bit more
    a
    1 reply · 2 participants
  • s

    Shawn

    01/26/2018, 12:43 AM
    @adam-mcneilly that’s odd, I just pasted that into my editor and it’s not complaining
    a
    4 replies · 2 participants
  • o

    oday

    01/28/2018, 12:15 PM
    hey, why is this happening? http://snpy.in/2eRFRN
    a
    o
    +2
    7 replies · 5 participants
  • o

    oday

    01/29/2018, 8:16 AM
    ok here we go, link works now, why is this happening? http://snpy.in/GzpxU4 it’s when I launch my first command in the kotlin interpreter
    i
    3 replies · 2 participants
  • v

    vicpinm

    01/29/2018, 2:25 PM
    Hi, I'm writting my first annotation processor in Kotlin. I need to check the type of a annotated property in order to know if is optional or not. I have something like @MyAnnotation var property: String? But I don't know if it can be done. I have an instance of Element class from javax.lang.model.element packpage in my annotation processor. I can do element.asType() to get the type of my property, but I don't know how to check if this property is optional or not.
    w
    o
    +1
    5 replies · 4 participants
  • w

    wineluis

    01/29/2018, 9:09 PM
    even simple lang like Go can do it ^^
    r
    1 reply · 2 participants
  • b

    bdawg.io

    01/30/2018, 1:03 AM
    enum valueOf method Am I able to modify the behavior of an enum’s
    valueOf
    ? Or will it only ever consider the
    name
    property on the specified enum?
    t
    3 replies · 2 participants
  • l

    Leeeloo Minai

    01/30/2018, 8:25 PM
    From what I read the .then(...) there is an extension function, right? What is the reason it was put there as such instead of inside the Filter interface as a member with implementation?
    a
    d
    2 replies · 3 participants
  • t

    tateisu

    01/31/2018, 11:10 AM
    Question. Kotlin requests to check nullable member property every time refer to it even it is initialized in same function. Is there a way to avoid this?
    // I have to copy to local variable
    fun menberFunction(){
        var x = this.x
        if( x == null ){
            x = X()
            this.x=x
        }
        codeUsingX(x)
        // this.x may reset to null outside of this function.
    }
    
    // I want just using  this.x
    fun menberFunction(){
        if( x == null ) x = X()
        codeUsingX(x)
        // this.x may reset to null outside of this function.
    }
    a
    17 replies · 2 participants
Powered by Linen
Title
t

tateisu

01/31/2018, 11:10 AM
Question. Kotlin requests to check nullable member property every time refer to it even it is initialized in same function. Is there a way to avoid this?
// I have to copy to local variable
fun menberFunction(){
    var x = this.x
    if( x == null ){
        x = X()
        this.x=x
    }
    codeUsingX(x)
    // this.x may reset to null outside of this function.
}

// I want just using  this.x
fun menberFunction(){
    if( x == null ) x = X()
    codeUsingX(x)
    // this.x may reset to null outside of this function.
}
a

Andreas Sinz

01/31/2018, 11:16 AM
do it this way:
val x = this.x ?: X()
then you have an immutable non-nullable
X
t

tateisu

01/31/2018, 11:17 AM
it's same thing about using local copy. also I still have to write
this.x=x
a

Andreas Sinz

01/31/2018, 11:18 AM
what are you trying to accomplish?
t

tateisu

01/31/2018, 11:20 AM
don't create local copy, just use this.x.
a

Andreas Sinz

01/31/2018, 11:22 AM
but you need to assign a new value when its
null
and afterwards use it like a non-nullable var?
t

tateisu

01/31/2018, 11:26 AM
If it is obvious that this.x is not manipulated externally while executing the function, It would be nice to have a way to express it. That way we do not need extra copies or null checks or wrapper function.
a

Andreas Sinz

01/31/2018, 11:37 AM
the compiler is not able to infer that
this.x
isn't manipulated during the execution of the functions
why is
this.x
nullable?
t

tateisu

01/31/2018, 11:38 AM
this.x may reset to null outside of this function. it may use many system resource.
If the compiler can not handle this problem correctly at this time, we should compromise with local copy
a

Andreas Sinz

01/31/2018, 11:44 AM
yes, the easiest way is to use a local property. I'm curious: What system resources are you using, that you need to be able to go back and forth between
null
and
X()
t

tateisu

01/31/2018, 11:55 AM
Actually, X will be given different arguments each time it is created. The lifecycle of caller and X is very different. I think that it is reasonable to use Nullable in this case
a

Andreas Sinz

01/31/2018, 1:40 PM
that sounds like you are violating the Single Responsibility Principle
t

tateisu

02/01/2018, 6:12 AM
I am not interested in your religion.
a

Andreas Sinz

02/01/2018, 7:51 AM
lol
its not about "religion", its about good software design
View count: 3