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

    Carrascado

    08/01/2020, 3:43 PM
    I have programmed a mini-class for practicing Kotlin. I'm getting really confused with how I could properly initialize these class properties without repeating the operations. I'm trying that whenever you initialize the
    Time
    class with more than 59 minutes, or more than 59 seconds, it automatically gets adjusted
    d
    m
    • 3
    • 22
  • r

    Raphael Ndonga

    08/02/2020, 11:43 AM
    Hello devs! Any advice for a kotlin android developer who wants to get started using KTOR?
    t
    • 2
    • 7
  • s

    spicyspiders

    08/03/2020, 8:52 AM
    What am I doing wrong here, is it not possible to use ternery inside Triple?
    return Triple((adults > 0 ? adults : null), (children > 0 ? children : null), (infants > 0 ? infants : null))
    d
    • 2
    • 3
  • d

    Dan O'Brien

    08/05/2020, 12:22 AM
    does anyone have an example of merging 2 Lists of Objects? I have Object A and Object B, I can match them on the same key but then I want to nest Object B under a deeper key in Object A
    r
    • 2
    • 3
  • c

    Carrascado

    08/05/2020, 5:06 PM
    Why do lateinit properties have to be var? I'd like the property to not be mutable after it's initialized
    n
    c
    • 3
    • 39
  • m

    Mattias Flodin

    08/06/2020, 5:37 PM
    Hi, in the section on Declaration-site variance here https://kotlinlang.org/docs/reference/generics.html#declaration-site-variance there is an example
    interface Source<out T> {
        fun nextT(): T
    }
    
    fun demo(strs: Source<String>) {
        val objects: Source<Any> = strs // This is OK, since T is an out-parameter
        // ...
    }
    If I replace String by a type argument T, it no longer compiles and says "Type mismatch: inferred type is Source<T> but Source<Any> was expected". Why is that? I had expected it work just like in the example.
    n
    a
    t
    • 4
    • 11
  • b

    blue

    08/07/2020, 5:18 AM
    message has been deleted
    :stackoverflow: 2
    • 1
    • 1
  • h

    huehnerlady

    08/07/2020, 5:49 AM
    Hi, TL;DR I have data classes that contain nullables but no default values. can I add these default values retrospectively without touching the data class itself, like through an extention function or something in that direction? long version: I have a lib which includes generated data classes. Unfortunately this lib has a lot of nullables and for test purpose I need to instantiate those classes. That means that I have a constructor with currently 51 nulls. I f I fill one value in and then add some data to the generated class, the parameter order might shift and then it is a pain to find out in which of the 51 positions I need to add the value. It is a real paini, it is on the radar of the generator to add default values, but for now I cannot change this behaviour in the data class directly. Is there a way to add add these default values retrospectively without touching the data class itself, like through an extention function or something in that direction I can use in the meantime?? it might be hacky but I cannot imagine it being as hacky as 50/51 null values per test case 😓
    s
    m
    +2
    • 5
    • 11
  • d

    Daniele B

    08/07/2020, 11:22 AM
    A field can have 5 different Strings values. Lets say: “APPLE”, “ORANGE”, “PEAR”, “BANANA”, “PEACH”. What is the best way in Kotlin to define such a variable type?
    :thread-please: 1
    r
    o
    t
    • 4
    • 6
  • d

    Daniele B

    08/07/2020, 11:24 AM
    So, basically I would like the field to be a String, but restricting it only to one of these 5
    :thread-please: 2
    e
    • 2
    • 4
  • s

    Sudhir Singh Khanger

    08/08/2020, 11:11 AM
    I came across two sets of code one where top-level-variable and an object is used and one where companion object is used. I am wondering which one is better.
    private val someTopLevelFunction = SomeClass.Builder().build()
    
    object SomeObject { val someObjectVariable by lazy { someTopLevelFunction().someMethod(someParam) } }
    The other option is to use companion object.
    interface SomeInterface {
        companion object {
            fun someTopLevelFunction() = SomeClass.Builder().build()
        }
    }
    These would be called as
    SomeObject.someObjectVariable()
    and
    SomeInterface.someTopLevelFunction()
    . As far as I understand top-level functions they can be accessed directly from anywhere which may pollute auto-completion. But if they are private then that issue won't happen. At the same time, I am not clear what would be their advantage if they are declared as private. Which one would you prefer? And why?
    n
    • 2
    • 5
  • d

    Daniele B

    08/08/2020, 4:16 PM
    I have an Enum:
    enum class ViewTypes {
        TODAY,
        LAST_7_DAYS,
        LAST_14_DAYS,
        TOTAL,
    }
    and I would like to loop through it:
    for (viewType in ViewTypes) {
    ...
    }
    but it gives me an error:
    Classifier 'ViewTypes' does not have a companion object, and thus must be initialized here
    :thread-please: 2
    n
    o
    • 3
    • 3
  • d

    Daniele B

    08/08/2020, 4:16 PM
    what is the correct way?
    :thread-please: 2
    o
    • 2
    • 1
  • d

    Daniele B

    08/08/2020, 4:18 PM
    oh, I found the way:
    enumValues<ViewTypes>().forEach {
    ...
    }
    :thread-please: 2
    o
    • 2
    • 2
  • d

    Daniele B

    08/08/2020, 7:01 PM
    Is it true than in Kotlin a function cannot return multiple values?!?!?
    n
    r
    • 3
    • 3
  • r

    Ruckus

    08/08/2020, 8:50 PM
    What would be the most "correct" way to add two `UByte`s together without overflow (e.g.
    0xA3u + 0x91u
    becomes
    0xFFu
    )? And the same for subtraction (e.g.
    0x34u - 0x51u
    becomes
    0x00u
    ).
    d
    • 2
    • 5
  • d

    Daniele B

    08/08/2020, 9:55 PM
    mylist.sortedBy{it.quantity}.map {
    	NewElement(
    		position = (how to retrieve the index of the element in the sorted list?)
    		name = it.name,
    		quantity = it.quantity
    	)
    }
    is there any way to retrieve the index of the element?
    n
    • 2
    • 14
  • c

    cookiecookiecookie

    08/09/2020, 12:11 AM
    is it possible to reference a Subclass’ override fun implemention within a parent sealed class fun?
    r
    • 2
    • 1
  • c

    cookiecookiecookie

    08/09/2020, 12:11 AM
    apologies if this is a dumb question i’m very new
    r
    • 2
    • 1
  • d

    Dan O'Brien

    08/09/2020, 11:58 PM
    does anyone have some good links/tutorials/books on testing with kotlin? I don’t have a Java background and have been working on an intellij plugin based on other plugins. I have a lot of functionality working but now I have no idea how to go and write tests for coverage
    n
    m
    • 3
    • 5
  • d

    Daniele B

    08/10/2020, 2:04 PM
    val resp = webservicesAPI.fetchLatest()
            resp?.let {
                repository.name = resp.name   
                repository.lastUpdate = resp.lastUpdate   
            }
    d
    m
    j
    • 4
    • 16
  • d

    Daniele B

    08/10/2020, 11:42 PM
    I have two lists: listA = listOf<ObjA>(…..) listB = listOf<ObjB>(…..) ObjA and ObjB have a common field called “fieldC” how can filter listA, so that it removes any ObjA object whose “fieldC” is not present in any element of listB?
    d
    e
    n
    • 4
    • 11
  • m

    Miguel

    08/11/2020, 1:52 PM
    Hello, I'm getting really confused about a type mismatch error:
    abstract class X
    data class Y() : X(), Serializable
    data class Z() : X(), Serializable
    
    var firstValue: X = Y() -> Type mismatch
    var secondValue: X = Z() -> Type mismatch
    Is there something I am missing?
    d
    d
    s
    • 4
    • 5
  • t

    tseisel

    08/11/2020, 4:54 PM
    Is there any convention for package names ? Should we stick to Java conventions (for example
    reverse.domain.name.myapp.mymodule
    ), or is it possible to use shorter names such as
    myapp.mymodule
    ?
    n
    f
    a
    • 4
    • 4
  • t

    tipsy

    08/12/2020, 12:44 PM
    what's the easiest way of making this interoperable with java?
    class MyClass(val arg1: String, val arg2: String? = null)
    (making it generate two constructors)
    d
    j
    n
    • 4
    • 10
  • k

    Karlo Lozovina

    08/12/2020, 8:11 PM
    Can I assign to
    var
    variable that's declared outside of an
    measureNanoTime
    block? It doesn't seem to work...
    n
    e
    f
    • 4
    • 3
  • s

    Sudhir Singh Khanger

    08/13/2020, 4:45 AM
    I am an Android developer and my web development knowledge in non-existent. I want to explore Kotlin/JS. Do I need to be proficient in JS? HTML & CSS? Is there any small course or tutorial that you guys could recommend to me to get started with the essentials?
    r
    c
    j
    • 4
    • 7
  • d

    Daniele B

    08/13/2020, 2:24 PM
    I have 2 different lists: listA has objA elements, with these properties: country, language listB has objB elements, with these properties: country, population I would like to merge them by creating listC, with properties: country, language, population How can I do that?
    d
    n
    • 3
    • 3
  • k

    Karlo Lozovina

    08/13/2020, 3:05 PM
    memory wise, are companion object more efficient? will all class instances share a single companion object or is a separate one created for every instance?
    s
    n
    +2
    • 5
    • 9
  • s

    Slackbot

    08/13/2020, 8:42 PM
    This message was deleted.
    n
    e
    • 3
    • 3
Powered by Linen
Title
s

Slackbot

08/13/2020, 8:42 PM
This message was deleted.
n

nanodeath

08/13/2020, 8:44 PM
it's basically a
fold
that's capable of early return
validatedFold
?
e

Esa

08/13/2020, 10:31 PM
Hm, yeah that seems sensible. Thanks 😃
View count: 1