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
announcements
  • z

    ziggy42

    03/19/2018, 12:11 PM
    I have some listeners:
    typealias Listener<T> = (T) -> Unit
    and for each
    T
    that I accept I need to store the list of registered listeners:
    val userListeners = mutableListOf<Listener<User>>()
    . But this forces me to have N lists for N possible
    T
    . Is there a better way?
    s
    a
    • 3
    • 4
  • p

    poohbar

    03/19/2018, 1:54 PM
    This is very interesting:
    Class<PersonKotlin> klass1 = PersonKotlin.class;
    KClass<PersonKotlin> kotlinClass1 = JvmClassMappingKt.getKotlinClass(klass1);
    
    Class<PersonJava> klass2 = PersonJava.class;
    KClass<PersonKotlin> kotlinClass2 = JvmClassMappingKt.getKotlinClass(klass2);  // error
    Although the types of
    klass1
    and
    klass2
    are identical the compiler recognizes that I can't call
    getKotlinClass
    on a Java class. How does it do that?
    d
    • 2
    • 6
  • m

    Michael

    03/19/2018, 2:18 PM
    So, I have some legacy java code that I don’t want to touch, but I would like to add some Kotlin extensions around it to make it easier to work with. The terrible java side (as far as I am constructing it) looks like
    Criteria criteria = new Criteria(new Filter(new Filter.FilterConditions("field", "value", FilterConditionOperator.EQ)))
    The “FilterConditionOperator” thing really annoys me - and I don’t think I’ve seen a way to sort of “force a static `import`“, right? Would the “idiomatic Kotlin” way to go be maybe to make an infix function for each of the enum values, as a FilterConditionFactory?
    e
    • 2
    • 2
  • z

    ziggy42

    03/19/2018, 2:40 PM
    Why can't I check at runtime if
    Listener<T>
    is
    Listener<User>
    ?
    d
    k
    a
    • 4
    • 34
  • k

    karelpeeters

    03/19/2018, 3:26 PM
    @Michael Define the function in the dsl class?
    m
    a
    m
    • 4
    • 5
  • n

    nfrankel

    03/19/2018, 3:56 PM
    anyone knows who manages https://kotlin.link/?
  • s

    Situations

    03/19/2018, 4:11 PM
    Anybody here have a hard time with compiling big projects in Intellij using kotlin/gradle? I mean like 3-10 minute compile times.
    e
    • 2
    • 1
  • m

    Michael

    03/19/2018, 4:39 PM
    (Mind you, I haven’t use Gradle!)
    n
    p
    +4
    • 7
    • 19
  • p

    poohbar

    03/19/2018, 5:04 PM
    how can I get corresponding
    java.lang.Class
    from
    kotlin.reflect.KClass
    ?
    d
    • 2
    • 32
  • s

    SrSouza

    03/20/2018, 7:10 AM
    is possible to make a binding of Flutter Engine to Kotlin Native?
    a
    s
    • 3
    • 2
  • y

    yusuf3000

    03/20/2018, 8:33 AM
    Beginner kotlin developer here. In swift land, when I wanted to create constants I would declare an enum which contains static variables:
    enum Constants {
        static let randomNumber: Int = 42
        static let randomString = "42"
        static let randomNonPrimitiveType = CustomClass(number: 42)
    }
    Then use it like so:
    let constantsUse = Constants.randomNonPrimitiveType
    What’s the best way to do this in Kotlin? Especially if the constant is a non primitive type.
    a
    • 2
    • 4
  • r

    robstoll

    03/20/2018, 11:50 AM
    oho... interesting 🙂
    g
    • 2
    • 1
  • g

    GarouDan

    03/20/2018, 2:39 PM
    So my question is, someone knows if we have an starter project to create multiplatform kotlin apps? Or other projects like this kotlinconf app with multiplataform branches (using all kotlin possibilities)?
    a
    • 2
    • 1
  • m

    mutexkid

    03/20/2018, 6:50 PM
    for the sample, “singletonReadOnlyList”, and for the documentation: “..an immutable list containing only..”
    🤔 1
    a
    k
    • 3
    • 14
  • g

    GarouDan

    03/20/2018, 8:57 PM
    Hi guys. Someone knows a good way (preferably using IntelliJ) to convert spaces identation to tabs in the entire project?
    :stackoverflow: 1
    c
    • 2
    • 1
  • d

    Dominik

    03/20/2018, 9:17 PM
    Hi fellow kotlin devs, does anyone of you know if its possible to put class level annotations on a global function? Do I miss something or is it simply not possible?
    a
    • 2
    • 2
  • d

    Dominik

    03/20/2018, 9:22 PM
    @Danilo You can add a formatter profile containing just that rule and reformat the whole project.
    g
    • 2
    • 1
  • k

    Karolo

    03/20/2018, 11:17 PM
    class Conference<T>(val mainOrganizer: T) { fun addSpeaker(speaker: (? extends T)) { } }
    e
    • 2
    • 1
  • j

    jmschultz

    03/20/2018, 11:20 PM
    My coworker Erica just filed a really interesting issue if anybody feels like checking it out:
    g
    k
    • 3
    • 6
  • a

    albertgao

    03/21/2018, 12:28 AM
    In my package
    a.b.c
    , I have an
    abstract
    class named
    BaseUser
    which I use it to share some code. But I want this class
    BaseUser
    to be internal in the
    a.b.c
    . Such that, I can create
    User:BaseUser
    in
    a.b.c
    . But the user of package can’t refer
    a.b.c.BaseUser
    . I tried the
    protected
    modifier. Not work, when I try to build, it said
    This type is final, so it cannot be inherited from
    ,
    Cannot access 'BaseUser': it is protected in 'a.b.c'
    and
    'public' subclass exposes its 'protected (in ?)' supertype BaseUser
    . No luck with
    internal
    too How to make this work? 🤔
    g
    g
    • 3
    • 5
  • b

    benny.huo

    03/21/2018, 8:37 AM
    Any ideas to solve the problem? I have also tried the IntelliJ 2018.1 or Kotlin plugin versions from 1.2.10 to 1.2.40dev and none of them works.
    c
    • 2
    • 2
  • e

    edwardwongtl

    03/21/2018, 10:21 AM
    The only change is just
    Exception e
    ->
    e: Exception
    s
    m
    m
    • 4
    • 8
  • b

    brescia123

    03/21/2018, 10:41 AM
    Hello everyone! is there a way to use Kotlin Preconditions checkNotNull() and have a smart cast for the checked variable?
    🚔 1
    h
    • 2
    • 2
  • s

    Slackbot

    03/21/2018, 11:25 AM
    This message was deleted.
    🚨 1
    👮‍♀️ 1
    👮‍♂️ 1
    💰 1
    🚓 4
    k
    i
    +2
    • 5
    • 4
  • k

    karelpeeters

    03/21/2018, 2:13 PM
    Is there a way to pass an array to a function that takes a vararg parameter? The spread operator passes a copy of the array, not the array itself. I encountered this problem when reading the docs of `Arrays.asList`:
    Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.)
    But it looks like using this property is not possible from Kotlin since you can't pass the real array.
    m
    d
    s
    • 4
    • 4
  • j

    janvladimirmostert

    03/21/2018, 2:50 PM
    what is the correct way to wait for a thread to finish? Is this ok or is there a better way to do it ?
    val mythread = thread { .... some code here that runs in this thread ... }
    ... more code here ...
    while (mythread.isAlive) {
        Thread.sleep(10)
    }
    do something else that depends on things setup during the thread
    s
    c
    • 3
    • 7
  • r

    robstoll

    03/21/2018, 4:22 PM
    is there are nicer way of writting the following:
    val list = mutableListOf()
    var parent = map[current]
    while(parent != null){
      list.add(parent)
      parent = map[parent]
    }
    I have the feeling there is a functional equivalent but I cannot come up with what it is
    a
    d
    d
    • 4
    • 20
  • s

    Slackbot

    03/21/2018, 6:49 PM
    This message was deleted.
    d
    t
    • 3
    • 2
  • a

    adams2

    03/22/2018, 1:24 AM
    should I do
    sealed class Foo { object BAR : Foo() }
    or
    sealed class Foo { object Bar : Foo() }
    2️⃣ 7
    k
    • 2
    • 1
  • r

    romster

    03/22/2018, 8:17 AM
    Hello guys! Could you please recommend sources where to read/listen about Kotlin code structure? As I can see Kotlin doesn't have "package" as a first-class citizen (no package-private access to members) It suggests to use "modules" instead, but they are slightly different thing, and I haven't seen any "best practices" of using Kotlin modules.
    c
    f
    • 3
    • 15
Powered by Linen
Title
r

romster

03/22/2018, 8:17 AM
Hello guys! Could you please recommend sources where to read/listen about Kotlin code structure? As I can see Kotlin doesn't have "package" as a first-class citizen (no package-private access to members) It suggests to use "modules" instead, but they are slightly different thing, and I haven't seen any "best practices" of using Kotlin modules.
c

Czar

03/22/2018, 9:50 AM
http://kotlinlang.org/docs/reference/coding-conventions.html#source-code-organization
r

romster

03/22/2018, 10:17 AM
Thank you, but there is nothing about modules and "internal" visibility best-practices there.
c

Czar

03/22/2018, 10:18 AM
Sorry, I don't understand what you mean then.
internal is the same as package, but with additional restriction to the same jar
r

romster

03/22/2018, 10:19 AM
So it is useful only for libraries developers, isn't it?
c

Czar

03/22/2018, 10:34 AM
why? within project it acts as package-private
f

Fleshgrinder

03/22/2018, 10:35 AM
But requires special build setup with separation into different directories and what not. Package private is a little easier to apply ad-hoc.
r

romster

03/22/2018, 10:36 AM
Except the code organisation, that package-stuff in java made some methods more testable without necessity to make them public. Maybe it wasn't idiomatic, but it was a very common approach. Kotlin closes this 'backdoor', but doesn't offer any way to test not-public methods, as I can see.
c

Czar

03/22/2018, 10:41 AM
generally, I believe, private methods should not be tested, if they have to be tested - something is wrong in the design or you're "overtesting" things.
f

Fleshgrinder

03/22/2018, 10:42 AM
You can easily access your internal stuff in your tests if you set it up with a dedicated module and I agree private stuff should not be tested.
r

romster

03/22/2018, 11:07 AM
Well, thank you, guys. But I can't agree with this position, because I prefer to limit public API and to decompose one big method into small 'private' (not only class-private) pieces (which could be easily tested, but it's not allowed). This approach with "do not test private method, or make them public" seems restriction-driven to me. Anyway, do you know any open-source project with Kotlin modules which you can call "reference-implementation" of good structure ?
c

Czar

03/22/2018, 2:53 PM
There are no "official" ones, but you could take a look at cbeust's #kobalt build system, or #arrow library or any other popular opensource app/lib written in kotlin, plenty of them on GitHub
And it's not really
do not test private method, or make them public
It's more like "Test all public methods, so that private ones don't need to be tested. If by testing the public method you cannot ensure correctness, and absolutely need to test private methods, then most probably there's something dangerously wrong in the design." I understand that
internal
stuff is not truly private and should be tested, that just means: keep your tests in the same module as your code.
e.g. in my project (I use gradle) if I have internal class (or a class with internal methods) and I'm testing it, this setup works perfectly fine:
/src/main/kotlin/com/example/InternalClass.kt
/src/test/kotlin/com/example/InternalClassTest.kt
f

Fleshgrinder

03/22/2018, 5:29 PM
@romster the last example from @Czar show exactly what I was talking about. With private I really meant private and tiny helpers that are only there to make the code more readable, hence, easy to test automatically via the public stuff that uses them. However,
internal
is different and you can easily test them. As I said, it's more cumbersome to set up compared to a proper package visibility and I would love to see it in Kotlin but right now that's what we have:
/lib/src/main/kotlin/com/example/PublicClass.kt
/lib/src/main/kotlin/com/example/InternalClass.kt
/lib/src/test/kotlin/com/example/PublicClassTest.kt
/lib/src/test/kotlin/com/example/InternalClassTest.kt

/app/src/main/kotlin/com/example/NoAccessToInternalClass.kt
View count: 2