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

    Michael

    06/26/2018, 2:29 PM
    Hey folks, I’ve got a method that takes, as one of its arguments,
    func: Function<Foo, Bar>
    . Elsewhere in the code, when calling it, I’m trying to pass in
    { it.baz }
    where it is type Foo, and baz is type Bar. But it’s giving me
    unresolved reference: it
    and saying that the type of the lambda is
    () -> [ERROR:]
    . If I prefix it with the word Function,
    Function { it.baz}
    it works.
    a
    • 2
    • 4
  • r

    Ron K

    06/26/2018, 2:46 PM
    My first guess was to define a
    fun Map<Any, Any>.cast(value: RubyHash) = value.toMap()
    . But this doesn't work.
    a
    u
    • 3
    • 8
  • h

    Hamza

    06/26/2018, 3:45 PM
    Hey everyone! 👋 is there a specific channel for like recruiting or something?
    s
    c
    • 3
    • 3
  • j

    Jose Antonio Jimenez

    06/26/2018, 4:44 PM
    I have the feeling that channels/coroutines is too low level
    l
    • 2
    • 3
  • r

    rook

    06/26/2018, 5:54 PM
    @Hamza That’s pretty misleading. They’ve said multiple times (including at KotlinConf) that experimental just means they haven’t finalized the API and that it’s fine because it’s backward-compatible back to 1.1
    ☝️ 1
    👍 1
    h
    • 2
    • 1
  • i

    ikt

    06/26/2018, 9:25 PM
    Does Kotlin/Common provide a "Timesamp" class (getNow(), to'String(), compare) that works for both Kotlin/Js and Kotlin/JVM ?
    🇳🇴 1
    o
    • 2
    • 2
  • w

    Wahib

    06/26/2018, 10:14 PM
    Does anyone know if I can apply
    when
    on a
    pair
    type? Finding it a bit difficult to make it work. Something like:
    val pair: Pair<StatusAType, StatusBType> = ...
    when(pair) {
                is (StatusA, StatusB) -> {
    
                }
    
            }
    It is showing an error
    Type Expected
    in IDE
    s
    j
    +2
    • 5
    • 8
  • j

    jacob

    06/26/2018, 11:25 PM
    @Wahib I don't think you can use pattern matching quite like that in Kotlin. It doesn't do destructuring that way. so you can pattern match on the Pair constructor but not the values inside it that way.
    w
    • 2
    • 2
  • e

    edwardwongtl

    06/27/2018, 8:20 AM
    Can anyone reason about this weird bug?
    enum class A {
      A, B
    }
    
    enum class B {
      A, B
    }
    
    fun main(args: Array<String>) {
      val a = A.A
    
      when (a) {
        A.A -> println("A.A")
        A.B -> println("A.B")
        B.A -> println("B.A") // hit this branch
        B.B -> println("B.B")
      }
    }
    🤨 2
    l
    • 2
    • 2
  • i

    ikt

    06/27/2018, 9:26 AM
    class Async_load<WaitT, ErrorT, DoneT> (
            val w: WaitT,
            f: Async_load<WaitT, ErrorT, DoneT>.(WaitT) -> Unit) {
    
        var state = Async_load_state.Wait<WaitT, ErrorT, DoneT>(w)
        init { f(w) }
    
        fun is_done(): Boolean {
            return when(state) {
                is Async_load_state.Error<*, *, *> -> true
                is Async_load_state.Done<*, *, *> -> true
                else -> false
            }
        }
    }
    Is there a way to simplify this? I do not want to repeat <WaitT, ErrorT, DoneT> all the time
    g
    • 2
    • 4
  • i

    ikt

    06/27/2018, 9:26 AM
    The full code is:
    sealed class Async_load_state<WaitT, ErrorT, DoneT> () {
        class Wait<WaitT, ErrorT, DoneT>(val wait: WaitT): Async_load_state<WaitT, ErrorT, DoneT>() {}
        class Error<WaitT, ErrorT, DoneT>(val error: ErrorT): Async_load_state<WaitT, ErrorT, DoneT>() {}
        class Done<WaitT, ErrorT, DoneT>(val done: DoneT): Async_load_state<WaitT, ErrorT, DoneT>() {}
    
    }
    
    class Async_load<WaitT, ErrorT, DoneT> (
            val w: WaitT,
            f: Async_load<WaitT, ErrorT, DoneT>.(WaitT) -> Unit) {
    
        var state = Async_load_state.Wait<WaitT, ErrorT, DoneT>(w)
        init { f(w) }
    
        fun is_done(): Boolean {
            return when(state) {
                is Async_load_state.Error<*, *, *> -> true
                is Async_load_state.Done<*, *, *> -> true
                else -> false
            }
        }
    }
    r
    • 2
    • 1
  • m

    Mathias Brandt

    06/27/2018, 9:50 AM
    Basic Kotlin question. Am I right that this:
    var matches = liveMatches.value
    if(matches == null) {
        matches = ArrayList()
    }
    Can be shortened to this:
    var matches = liveMatches.value ?: ArrayList()
    :yes: 3
    g
    • 2
    • 2
  • d

    Dariusz Sadowski

    06/27/2018, 9:54 AM
    Even shorter
    var matches = liveMatches.value ?: mutableListOf()
    d
    • 2
    • 2
  • m

    Mathias Brandt

    06/27/2018, 10:52 AM
    Another one: I have defined a field
    val liveChallenge = MutableLiveData<Challenge>()
    . I want to expose it as a
    LiveData<Challenge>
    . Should I just make the field private and write a getter,
    fun getLiveChallenge(): LiveData<Challenge> = liveChallenge
    (like I would do in Java) or is there a smarter way?
    d
    g
    • 3
    • 5
  • s

    spierce7

    06/27/2018, 4:46 PM
    What is JPS? There are tickets about adding JPS compilation in 1.2.60: https://github.com/JetBrains/kotlin/releases/tag/v1.2.60-eap-7
    s
    k
    p
    • 4
    • 3
  • r

    Ruckus

    06/27/2018, 5:56 PM
    So, I have the following:
    val type: Class<T> = ...
    val clazz: Class<Any> = ...
    val superClazz = clazz.superclass  // is Class<in Any!>!
    // works fine
    if (type == clazz) ...
    // won't compile: Operator '==' cannot be applied
    if (type == superClazz) ...
    Why doesn't that work? Doesn't
    Class<in Any!>! : Any
    still hold true?
    r
    • 2
    • 7
  • s

    Slackbot

    06/28/2018, 7:52 AM
    This message was deleted.
    k
    w
    • 3
    • 2
  • d

    dalexander

    06/28/2018, 12:09 PM
    fun <T> Single<T>.sub(onNext: (T) -> Unit = { }): Disposable
    This would be a method that by default has an empty onNext.
    👍 1
    r
    • 2
    • 1
  • i

    igorvd

    06/28/2018, 12:55 PM
    is there an away to avoid the assertion operator to make the value type not null on the following case?
    if (key in map) {
            val value = map[key]!!
        }
    If I remove the assertion operator, the type of the
    value
    will be
    ?
    p
    g
    • 3
    • 3
  • r

    Ruckus

    06/28/2018, 5:14 PM
    This issue has been fixed for a while, but is still open: https://youtrack.jetbrains.com/issue/KT-17657 What is the proper way of bringing these up to the [JB] overlords to get it remedied?
    i
    • 2
    • 5
  • i

    ikt

    06/28/2018, 6:14 PM
    Does Kotlin/JS have a builtin json parser? If not, what library is suggested? (I'm already using Klaxson for Kotlin/JVM)
    j
    • 2
    • 1
  • s

    snowe

    06/28/2018, 8:32 PM
    is it desired behavior for reflection to be able to bypass kotlin null safety?
    a
    m
    g
    • 4
    • 22
  • f

    fatih.yalmanbas

    06/29/2018, 8:48 AM
    So that way, both from java and kotlin i would use same package and same class name (consistency problem)
    n
    e
    • 3
    • 7
  • n

    navi

    06/29/2018, 10:28 AM
    I have added a new issue on Youtrack https://youtrack.jetbrains.com/issue/KT-25180 Here I want to add a toMap() function to Kotlin Any class which will return a hashmap of the variables of the class mapped to their instance value.
    e
    l
    +2
    • 5
    • 16
  • m

    Mathias Brandt

    06/29/2018, 10:47 AM
    I have a list of
    Player
    objects. Each
    Player
    has a
    score: Int
    . I want to calculate the total score for all players. Can I do something like
    list.reduce { ... }
    directly, or do I have to use map as well, e.g.
    list.map { it.score }.reduce { sum, score -> sum + score }
    l
    • 2
    • 2
  • k

    karelpeeters

    06/29/2018, 1:12 PM
    There's no way to inherit constructor parameters from a parent class, right? I'm looking for something like this:
    open class Parent(val a: Int, val b: Int)
    class Child(val c: Int) : Parent(@inherit)
    Child(a = 1, b = 2; c = 3)
    d
    s
    r
    • 4
    • 9
  • i

    ikt

    06/30/2018, 3:06 AM
    inline fun foo ( ... ) // <-- this will inline all lambdas passed to foo, AND it will also inline all calls of foo ?
    g
    • 2
    • 1
  • r

    rahulrav

    07/01/2018, 12:41 AM
    Hi everyone.. I have a
    ktor
    question
    r
    • 2
    • 2
  • c

    CircuitRCAY

    07/01/2018, 12:51 AM
    Is there any ETA on pattern matching or destructuring in Kotlin any time soon?
    s
    k
    g
    • 4
    • 3
  • u

    ursus

    07/01/2018, 5:01 AM
    Are algebraic datatypes (sealed classes) good for api design when you cant tell of all the possible variants just by looking at the funcion? without looking at the type impl details
    k
    g
    • 3
    • 7
Powered by Linen
Title
u

ursus

07/01/2018, 5:01 AM
Are algebraic datatypes (sealed classes) good for api design when you cant tell of all the possible variants just by looking at the funcion? without looking at the type impl details
k

karelpeeters

07/01/2018, 8:30 AM
I don't really see a problem here: they're just like normal
open
classes with lots of possible types, just a bit restricted.
u

ursus

07/01/2018, 1:24 PM
Yes but you as a consumer dont know about, when compared to maybe monads you see Either you know what to do
k

karelpeeters

07/01/2018, 1:42 PM
Well you can always look at the docs or the source, right?
u

ursus

07/01/2018, 3:03 PM
Well sure, same as you can use ints for enums .. im just asking questions, not arguing
k

karelpeeters

07/01/2018, 3:05 PM
Haha sure, I just don't really get the problem. Of course you need to look at the docs if you want to know about a certain class.
g

gildor

07/01/2018, 3:32 PM
I think sealed classes is good option for such api. If you use when as expression compiler will notify you about forgotten cases and even if new class of sealed hierarchy was added later
u

ursus

07/01/2018, 3:56 PM
@karelpeeters the problem is imo discoverability, if every sealed class is unique and from call site you dont know its a hierarchy, then intuitively you dont know what to do, but guess when I think about it, enums have the same issue
View count: 5