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

    Stephan Schroeder

    02/14/2020, 5:03 PM
    how would you and extension function that removes a bunch of characters from a String?
    Fun String.removeChars(allTheCharsToRemove: String): String
    there is the String.replace(regex, replacement) that you could call with
    ""
    as second parameter. So I’d have to create a Regex from my
    allTheCharsToRemove
    (how? if that’d be your approach) or is there a better way?
    f
    r
    • 3
    • 2
  • l

    Luke

    02/14/2020, 6:41 PM
    Considering :
    inline fun <T> List<T>.applyOnEach(block: T.() -> Unit): List<T> = onEach { it?.block() }
    Is there a way to mark the receiver in
    T.() -> Unit
    as not nullable?
    j
    z
    m
    • 4
    • 8
  • b

    Buzz

    02/14/2020, 10:15 PM
    Does anyone here use Kotlin for mobile development?
    s
    b
    p
    • 4
    • 3
  • b

    Brian Dilley

    02/14/2020, 11:54 PM
    So what are the plans for 1.4? Is there somewhere I can read about the new IR and other significant changes?
    d
    • 2
    • 1
  • b

    Bruno_

    02/15/2020, 3:07 PM
    assuming that you have a typical module (repository, model, config and other stuff) do you expose the model class (the one you use in repo) or a DTO through a service/facade?
    c
    • 2
    • 2
  • b

    bod

    02/15/2020, 5:37 PM
    Hello, World! I'm looking at the kotlin.time package. I see there's a
    Duration
    abstraction but what about a Moment (aka, Date or Time stamp, or a point in time)? Is that what
    ClockMark
    represents?
    l
    l
    d
    • 4
    • 5
  • e

    ec

    02/15/2020, 8:32 PM
    What do you guys think about this kind of naming?
    typealias SessionId = String
    typealias TopicName = String
    fun unsubscribe(who: SessionId, from: TopicName) {..}
    fun subscribe(to: TopicName, handler: (Topic) -> Unit)
    👍 1
    👎 3
    l
    g
    +4
    • 7
    • 14
  • j

    Jeff

    02/16/2020, 9:48 AM
    Hi Guys. I am just trying out Multiplatform. I wanted to write a multiplatform library that connects to a P*ayment API*. The library should work on all platforms (Android, iOS, Windows, Javascript e.t.c). Any ideas on how I should go about this?
    👍 3
    c
    n
    • 3
    • 5
  • c

    Chills

    02/16/2020, 1:58 PM
    i did understand continuation , context but unable to understand these
    co-routine scopes
    for coroutines can someone explain me in
    layman
    terms. builders are used to create coroutines dispatchers to alter context.
    a
    s
    • 3
    • 4
  • c

    camkadev

    02/16/2020, 3:11 PM
    kotlin/native serialization
    Cannot infer type parameter
    what should i do at
    Serializer
    line
    31
    ? https://github.com/AlexanderShniperson/KotlinNativeSerialization
    Serializer.kt
    a
    • 2
    • 2
  • k

    krtko

    02/16/2020, 9:08 PM
    If all I want from reflection is
    any::class.simpleName
    , does that still require including the Kotlin Reflection jar?
    f
    d
    • 3
    • 8
  • e

    ersin_ertan

    02/17/2020, 12:42 AM
    Why doesn't
    package kotlin.reflect.jvm.internal.calls.Caller.checkArguments(args: Array<*>)
    respect functions with default parameter values? I don't want to include it in
    func.call(..., "alreadyHasADefaultValue")
    , and am stopped by
    throw IllegalArgumentException("Callable expects $arity arguments, but ${args.size} were provided.")
    d
    • 2
    • 2
  • g

    Gyuhyeon

    02/17/2020, 9:17 AM
    there's a method which takes a @FunctionalInterface as a parameter. I can see that passing a lambda like
    .filter{req: ClientRequest, next: ExchangeFunction ->
        // do something
    }
    works. However, what I'd like to do is pass a function reference instead of the lambda so that I don't have to actually put 50 lines of code inside the
    .filter
    call. I'm quite new to Kotlin and I have a very fragile understanding of higher order functions, so I have no idea how to achieve this. I thought making the method like this -
    private fun customFilter(request: ClientRequest, next: ExchangeFunction){
    
    }
    and calling like
    .filter(::customFilter)
    would work, but it didn't - IDE tells me
    Type mismatch.
    Required:
    ExchangeFilterFunction
    Found:
    KFunction2<@ParameterName ClientRequest, @ParameterName ExchangeFunction, Unit>
    It seems like I have to somehow implement the ExchangeFilterFunction.java which is a java
    @FunctionalInterface
    ...? Any advice is appreciated
    g
    • 2
    • 2
  • j

    Jakub

    02/17/2020, 10:15 AM
    What is more performant: 1.
    days.groupBy { it.date.year }.forEach { (year, daysOfYear) ->
        daysOfYear.groupBy { it.date.month }.forEach { (month, daysOfMonth) ->
          aysOfMonth.groupBy { it.date.weekNumber() }.forEach { (week, daysOfWeek) ->
    }}}
    2.
    days.groupBy { Triple(it.date.year, it.date.month, it.date.weekNumber()) }.forEach { (triple, days) ->
    }
    f
    j
    • 3
    • 4
  • h

    hooliooo

    02/17/2020, 12:48 PM
    Is there a recommended Kotlin library that focuses on geometry?
    a
    • 2
    • 3
  • c

    Chills

    02/17/2020, 6:35 PM
    i dont want to make caller have suspend the function on call to my function.
    h
    l
    • 3
    • 2
  • s

    suresh

    02/18/2020, 5:20 AM
    Hi Kotlin folks, is there any library or compiler plugin available to generate builders (java compatibility is very important) from Kotlin Data classes. SO has a lot of suggestions for this problem, but most of them requires way more boilerplate than expected.
    🤷 1
    t
    • 2
    • 2
  • o

    Ofir Bar

    02/18/2020, 8:50 AM
    Hey friends, Is there a point in reading Effective Java, with the movement to Kotlin?
    r
    a
    +2
    • 5
    • 9
  • t

    tjohnn

    02/18/2020, 11:35 AM
    Are kotlin SAM types Serializable? eg
    () -> Unit
    s
    • 2
    • 2
  • e

    eekboom

    02/18/2020, 11:53 AM
    Is there a Kotlin way of locale-specific sorting?
    fun sort(list: List<String>): List<String> {
        val collator = Collator.getInstance(Locale.GERMAN)
        collator.strength = Collator.PRIMARY
        return list.sortedWith(Comparator {a, b -> collator.compare(a, b)})
    }
    i
    b
    • 3
    • 3
  • g

    Gunslingor

    02/18/2020, 2:40 PM
    What's the correct way to import and use a DLL in kotlin and intelliJ using modules?
    • 1
    • 1
  • j

    Jan Stoltman

    02/18/2020, 3:20 PM
    Hey, is there a way to disable build warnings for a specific warning type? Eg. I would like to disable all
    EXPERIMENTAL_API_USAGE
    warnings in my project build log
    l
    d
    • 3
    • 8
  • m

    maxmello

    02/18/2020, 4:25 PM
    Is it possible to have the following:
    abstract class A<T : A<T>>(
        val id: Id<T>
    )
    
    class B(id: Id<B>) : A<B>(id)
    class C(id: Id<C>) : A<C>(id)
    I want to have polymorphism in a DB model, and the library provides type safe
    Id
    with generics. I want the superclass to have the id property and to restrict it to be of any subclass of itself. For this I have the
    A<T : A<T>>
    definition on top. Now, a subclass passes in its own type and only allows id to be of that type, making sure I never have
    Id<C>
    in a
    B
    class. But when trying to use that type system, I have the problem of recursive definition of the parameter of A. For example when mapping a list of instances of objects holding subclasses of A, I need to specify the type variable like this:
    SomeClassHoldingASubclasses(…)
    =>
    SomeClassHoldingASubclasses<A<A<A ... > > >
    It is not possible to do it like this:
    SomeClassHoldingASubclasses<*>
    “projections are not allowed on type arguments of functions and properties” If I remove the type variable from A, I cannot pass an id : Id<B> to the constructor of A. I’m trying to get this to work for quite some time now, but are these polymorphic / self-referencing type variables even possible? Should I use
    in
    or
    out
    (I tried but that also went weird fast)?
    a
    d
    • 3
    • 5
  • i

    Ian

    02/18/2020, 4:36 PM
    I'm getting this crazy
    java.lang.IllegalStateException: Backend Internal error: Exception during code generation
    exception while trying to compile my code after updating some Gradle dependencies to their most recent version: https://gist.github.com/sanity/d4d0a84155a52a26c09386b0cf9a9e27 Anyone have any ideas what it might be? Unfortunately it's blocking progress on my project.
    m
    • 2
    • 1
  • n

    Nikita Khlebushkin

    02/18/2020, 5:15 PM
    Hello, I have such method in a library:
    open fun track(event: String, properties: Map<Any?, *>?, options: Map<Any?, *>?): Unit
    How can I create a map that would have satisfy
    properties
    type?
    s
    s
    • 3
    • 8
  • j

    Jason

    02/19/2020, 6:07 AM
    What is difference between
    Transformations.map
    and
    Livedata.map
    ? Which one is better ? And what is usecase for it?
    a
    • 2
    • 2
  • c

    camkadev

    02/19/2020, 8:38 AM
    pls suggest any kotlin/native multiplatform logger
    a
    l
    • 3
    • 4
  • c

    CarlosP

    02/19/2020, 10:48 AM
    Hi guys. How can I override a variable of a derived interface type. I have the interfaces:
    interface ExperimentManagerDelegateCore {
    
    interface ExperimentManagerDelegate:ExperimentManagerDelegateCore {
    I have the classes:
    abstract class ExperimentManagerCore {
    
        open var delegate: WeakReference<out ExperimentManagerDelegateCore>? = null
    }
    
    class ExperimentManager():ExperimentManagerCore() {
    
        override var delegate: WeakReference<ExperimentManagerDelegate>? = null
    }
    but in
    ExperimentManager
    I got the error:
    Type of 'delegate' doesn't match the type of the overridden var-property 'public open var delegate: WeakReference? defined in ExperimentManager.ExperimentManagerCore'
    I thought I would fix it with
    out ExperimentManagerDelegateCore
    Any ideas?
    m
    • 2
    • 8
  • j

    Jan

    02/19/2020, 12:20 PM
    how do you write java
    0x1p10
    in kotlin?
    o
    m
    e
    • 4
    • 13
  • l

    Leon K

    02/19/2020, 12:48 PM
    is there a way to say "this function executes a lambda. If the lambda is
    suspend
    , make the function
    suspend
    , otherwise don't". I know the same thing can be achieved using
    inline fun
    , but I have to access private fields in this function so that's not an option
    🤔 1
    d
    • 2
    • 10
Powered by Linen
Title
l

Leon K

02/19/2020, 12:48 PM
is there a way to say "this function executes a lambda. If the lambda is
suspend
, make the function
suspend
, otherwise don't". I know the same thing can be achieved using
inline fun
, but I have to access private fields in this function so that's not an option
🤔 1
d

diesieben07

02/19/2020, 12:51 PM
You'll have to have two functions then, as a function signature changes if its
suspend
(it has to take an additional
Continuation
parameter)
What you can maybe do is have the public-facing function
inline
and encapsulate the private access in a
@PublishedApi internal fun
which does not suspend and doesn't use the lambda.
l

Leon K

02/19/2020, 1:07 PM
I'll try that, thanks! could this technically be something that we'll be able to do in the future? like, have an additional modifier or some other way to "inline" suspendability
d

diesieben07

02/19/2020, 1:10 PM
It would have to compile down to two functions. Suspending and non-suspending functions result it quite different bytecode.
l

Leon K

02/19/2020, 1:13 PM
that would be an option. another option would be to still inline them but to somehow allow access of private properties in inline functions. Another question: Why is it that this works if the class containing the inline function is private and is declared within the same file it's used? In that case i can refer to the private fields from my inline fun
d

diesieben07

02/19/2020, 1:17 PM
You have to keep in mind that inlining means just that: The function code is inlined into the code where it is called. That means a public inline function only has access to public things, because private things could not be accessed by whomever is calling that inline function.
l

Leon K

02/19/2020, 1:18 PM
this does make sense, altough I'd think it's pretty likely that JVM-stuff would allow this using some strange hacks (reflection would most likely work but would be a bad idea). But why does it work with private classes?
d

diesieben07

02/19/2020, 1:21 PM
Because if the inline function is
private
(or in a
private
class) then it can only ever be inline in the same class / file, so wherever it is inlined to also has access to the same private fields.
Also yes, you could work around it using reflection, but keep in mind that usually
private
fields are not part of your public API. If you could access them inside an
inline
function they suddenly would be, because the code of your inline function gets copied into other people's code.
l

Leon K

02/19/2020, 1:26 PM
okay, thanks!
View count: 3