https://kotlinlang.org logo
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
  • t

    thana

    07/11/2019, 8:28 AM
    what do you think about
    infix fun <T : Any?> T.`if`(condition: Boolean): Pair<Boolean, T> =
        Pair(condition, this)
    
    infix fun <T> Pair<Boolean, T>.`else`(block: () -> T) =
        if (component1()) component2()
        else block()
    p
    k
    • 3
    • 8
  • v

    ValV

    07/11/2019, 8:37 AM
    Can someone explain such thing: in Java code there is an interface
    Completer
    that has only one method (function)
    public abstract int complete(arg1, arg2, arg3)
    , so when a variable
    completer: Completer?
    is assigned, it's done via Java lambda
    completer = (arg1, arg2, arg3) -> { ... }
    Instead of creating (at least) anonymous object of type
    Completer
    and
    override
    -ing the
    complete
    method. Is it possible to do something like that in Kotlin, or I have to do
    object: Completer {
      override fun complete(arg1, arg2, arg3) { ... }
    }
    Why I can't do it with lambda -- because the compiler complains that
    completer
    must be of type
    Completer?
    , not
    (???, ???, ???) -> Unit
    p
    • 2
    • 2
  • k

    KindRoacher

    07/11/2019, 9:02 AM
    With 1.3 stable release, context switching using lambda expression is still marked as experimental. Would it be 'ok' to use inside a production app?
    k
    • 2
    • 1
  • e

    elect

    07/11/2019, 9:32 AM
    I'm trying to test if I successfully moved my project to JPMS by running manually
    jdeps
    , however I need to specify where to find the
    kotlin.stdlib
    module, is there a standard location for that or shall I search in my
    .m2
    local gradle cache?
    d
    • 2
    • 4
  • v

    ValV

    07/11/2019, 10:04 AM
    How to
    MutableList<String>
    ->
    Array<String>
    ?
    d
    d
    +2
    • 5
    • 10
  • e

    elect

    07/11/2019, 10:16 AM
    I guess I'm getting into a deadlock, I have a multi-release dependency and then I set the multi-release option
    manifest.attributes('Multi-Release': 'true')
    , but now
    jdeps
    complains:
    Error: kotlin-stdlib-1.3.41.jar is not a multi-release jar file but --multi-release option is set
    :thread-please: 1
    • 1
    • 3
  • a

    Alexpl292

    07/11/2019, 11:10 AM
    Hi! Is it possible to define an extension function in java code? https://stackoverflow.com/questions/56987823/define-kotlin-extension-function-in-java-code
    🇳🇴 1
    t
    • 2
    • 1
  • v

    ValV

    07/11/2019, 11:29 AM
    I have a class
    class MyClass {
      companion object {
        public fun run(args: Array<String>) {
          ...
        }
      }
      public fun main(args: Array<String>) {
        ...
      }
    }
    How to call
    run
    from
    main
    ?
    d
    p
    • 3
    • 20
  • i

    Ifvwm

    07/11/2019, 11:39 AM
    if I compile kotlin code with coroutine to jar, can I run it with java?
    t
    • 2
    • 1
  • x

    xenoterracide

    07/11/2019, 5:40 PM
    internal inline fun <reified T: BaseResource> find( criteria: (query: IQuery<Bundle>) -> IQuery<Bundle>): T? {
            val forResource = client.search<Bundle>().forResource(T::class.java)
    
            val entities = criteria.invoke(forResource).execute()
    
            if ( entities.isEmpty ) {
                <http://log.info|log.info>("entity {} was not found", T::class.simpleName)
                return null
            }
            if ( entities.total > 1 ) {
                <http://log.info|log.info>("too many {} found", T::class.simpleName)
                return null
            }
    
            return entities.entryFirstRep.resource as? T
        }
        internal fun findLocation(name: String): Location? {
            return find {it.where(Location.NAME.matches().value(name)) }
        }
    is there a method signature where I can just call
    where
    instead of
    it.where
    ?
    d
    • 2
    • 3
  • m

    mathew murphy

    07/11/2019, 5:44 PM
    Is there a way to tell Kotlin to convert all instances of
    java.lang.String
    to
    String?
    rather than
    String!
    within a specified context, so that I get errors or warnings if I try to do (say)
    resultset.getString(2).trim()
    ?
    🇳🇴 2
    g
    a
    • 3
    • 3
  • g

    groostav

    07/11/2019, 6:12 PM
    If I was to buy a new computer and I wanted the fastest possible compile times (specifically intelliJ/kotlin, though I do ofc have many other toolchains), what should be my focus point?
    s
    k
    +2
    • 5
    • 23
  • x

    xenoterracide

    07/11/2019, 8:36 PM
    is there something like an
    exists { Predicate } : Boolean
    on collections?
    m
    r
    • 3
    • 5
  • f

    Florian

    07/11/2019, 9:14 PM
    does kotlin not have single
    &
    and
    |
    boolean operator?
    :yes: 2
    v
    k
    +3
    • 6
    • 20
  • f

    Florian

    07/11/2019, 9:15 PM
    also, do
    &&
    and
    ||
    not have an
    operator fun
    ?
    k
    • 2
    • 14
  • v

    vlad.minaev

    07/12/2019, 10:52 AM
    In TypeScript there is a
    Pick
    type which lets you easily (and type-safe) define sub-shape:
    interface Todo {
        title: string;
        description: string;
        completed: boolean;
    }
    
    type TodoPreview = Pick<Todo, 'title' | 'completed'>; // you can only specify properties of Todo here
    
    const todo: TodoPreview = {
        title: 'Clean room',
        completed: false,
    };
    I'm trying to find out whether it's possible to achieve something like this in Kotlin
    k
    t
    a
    • 4
    • 12
  • c

    Chris

    07/12/2019, 1:58 PM
    Is this a known bug?
    k
    s
    • 3
    • 21
  • a

    Andrew Gazelka

    07/12/2019, 6:25 PM
    is there anyway to have something like this
    fun <T> wrapperFunc(block: (…) -> T): (…) -> T{
        println("wrapped")
        return block (…) -> T
    }
    and call it like
    val testFunc = wrapperFunc {
        fun(a: String) -> println("a is $a")
    }
    
    testFunc("abc")
    🇳🇴 1
    l
    s
    +2
    • 5
    • 8
  • p

    pavi2410

    07/12/2019, 6:50 PM
    Any lightweight Kotlin interpreter in the market?
    🇳🇴 1
    j
    k
    +2
    • 5
    • 19
  • h

    Hullaballoonatic

    07/12/2019, 7:25 PM
    is
    onEach
    more efficient than
    map
    ?
    s
    k
    • 3
    • 3
  • e

    Eric O'Connell

    07/12/2019, 7:57 PM
    If I have a function
    fun <T, R> Optional<T>.mapDefault(default: R, mapper: (T) -> R): R
    and call it with a default of a subtype of a
    sealed class
    e.g.
    maybeThing.mapDefault(Action.NoFoo) { thing -> Action.Foo }
    then this fails type checking because
    R
    in
    mapDefault
    is bound to
    Action.NoFoo
    — is there a good way around this besides calling
    maybeThing.mapDefault<Thing, Action>
    ?
    s
    • 2
    • 4
  • b

    bbaldino

    07/12/2019, 8:57 PM
    is there a way to expose a
    MutableMap
    externally as a
    Map
    without using a "wrapper" member? i.e.
    private val _data = mutableMapOf<Int, String>()
    val data: Map<Int, String>
        get() = _data
    j
    r
    k
    • 4
    • 8
  • t

    tipsy

    07/13/2019, 8:41 AM
    is there a more compact way to express this?
    init {
        require(level in 0..9) {
            "Valid range for parameter 'level' is 0 to 9"
        }
    }
    g
    r
    • 3
    • 19
  • c

    ciriti

    07/13/2019, 9:36 AM
    Hi all! do you know if there is a channel for kotlin multiplatform?
    d
    • 2
    • 2
  • e

    Eric Martori

    07/13/2019, 11:14 AM
    I have a doubt about
    sealed classes
    and method overloading. Imagine I have the following sealed class:
    sealed class State {
        object A : State()
        data class B(val i: Int) : State()
    }
    And the following methods defined:
    fun process(a: State.A): Unit {/**/}
    fun process(b: State.B): Unit {/**/}
    I should be able to call
    process(state)
    with an instance of
    State
    since the compiler knows that there are only two subtypes of it and that the method is defined for both of them. But it forces me to check the type (with a
    when
    for example):
    fun newState(state: State) = process(state) //doesn`t compile (None of the following functions can be called with the arguments supplied)
    fun newState(state: State) = when(state) { // this works but its really verbose 
        is State.A -> process(state)
        is State.B -> process(state)
    }
    s
    l
    • 3
    • 6
  • t

    turansky

    07/14/2019, 1:29 PM
    Is there any special KDoc tag for generics?
    -.kt
    s
    • 2
    • 1
  • f

    Fudge

    07/14/2019, 2:59 PM
    How can I make IntelliJ not complain about this for the entire project?
    g
    • 2
    • 1
  • v

    v0ldem0rt

    07/14/2019, 3:57 PM
    I think Kotlin is great for backend servers. We have been debating in our company to fully switch over to Kotlin; the only reason we are having trouble convincing orthodox Java folks is because they argue they have not seen Kotlin on backend and it's only designed for front-end codebase. It would be nice if Kotlin's website can showcase Backend server usecases. Because everything points to Androind usage right now
    t
    m
    +3
    • 6
    • 10
  • i

    Ifvwm

    07/14/2019, 4:22 PM
    how I can store the instances into a global list when the classes instantiate? in python it’s like alist = [] class T: def __init__(self): alist.append(self) then I can get all the instances of class T in alist
    k
    h
    g
    • 4
    • 15
  • a

    Alexjok

    07/14/2019, 8:18 PM
    How i can batch list in Kotlin?
    k
    • 2
    • 3
Powered by Linen
Title
a

Alexjok

07/14/2019, 8:18 PM
How i can batch list in Kotlin?
k

karelpeeters

07/14/2019, 8:18 PM
Can you elaborate?
a

Alexjok

07/14/2019, 8:24 PM
for example:
listOf(1,2,3,4,5,6,7,8,9,10)
            .map { element -> async { delay(1000); element * 2 }}
            .awaitAll()
            .forEach(::println)
How i can chunk it, send only 2 elements compute it in parallel and after send next 2 elements?
k

karelpeeters

07/14/2019, 8:25 PM
#coroutines
View count: 7