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

    Bernhard

    02/02/2019, 8:19 AM
    so @JvmField kinda generates metadata twice, once for kotlin and once for java?
    p
    • 2
    • 12
  • c

    CircuitRCAY

    02/02/2019, 10:09 AM
    What is Kotlin's stance on nested destructuring, similar to Scala's feature, like so:
    fun main(args: Array<String>) {
        val p1 = Pair(1, 2);
        val p2 = Pair(3, 4);
        val p3 = Pair(p1, p2);
        val (simple, destructuring) = p1
        //val ((nested, _), (destructuring, _)) = p3;
    }
    Hope you get back to me, thanks
    a
    s
    • 3
    • 2
  • h

    Hexa

    02/02/2019, 3:35 PM
    Does anyone have a list of Kotlin repo that requires contribution/PR?
    g
    • 2
    • 1
  • p

    pablisco

    02/02/2019, 4:01 PM
    What do the good people here prefer? https://twitter.com/pablisc0/status/1091715054070874113 😁
    s
    m
    • 3
    • 6
  • t

    tapchicoma

    02/02/2019, 5:05 PM
    Why in this example
    item1
    is highlighted with "Cannot infer a type for this parameter. Please specify it explicitly"? Is there a way to modify
    someWrapper()
    to remove this error?
    -.kt
    s
    m
    • 3
    • 4
  • m

    mhamade

    02/03/2019, 2:52 AM
    Why should you avoid @JvmStatic for a logger?
    m
    • 2
    • 2
  • n

    Nikky

    02/03/2019, 3:40 AM
    does anyone know of a kotlin or java library that can take ansi colored text (from running
    git diff
    process) and turn it into colored html output?
    g
    g
    a
    • 4
    • 6
  • j

    Joe

    02/03/2019, 6:38 AM
    I'm trying to reflectively access a property value of a data class like this:
    it::class.memberProperties.filter { prop -> prop.name == propertyName }.first().get(it)
    but getting the following compiler error:
    Out-projected type 'KProperty1<out DataClass!, Any?>' prohibits the use of 'public abstract fun get(receiver: T): R defined in kotlin.reflect.KProperty1'
    using
    it::class.java.getMethod("get${propertyName.capitalize()}").invoke(it)
    appears to work, but it seems like there should be a way to do this through KClass directly (and avoid having to generate the getter method name)?
    m
    • 2
    • 3
  • t

    tipsy

    02/03/2019, 9:30 AM
    i get this error when trying to switch from hamcrest to assertj:
    Error:Kotlin: [Internal Error] java.lang.IllegalStateException: Backend Internal error: Exception during code generation
    Cause: Back-end (JVM) Internal error: wrong code generated
    org.jetbrains.kotlin.codegen.CompilationException Back-end (JVM) Internal error: Couldn't transform method node:
    anyone know what's up?
    d
    g
    • 3
    • 11
  • n

    natpryce

    02/03/2019, 12:56 PM
    Does assertJ have overloads for primitive types? Hamcrest matchers are generics, so primitive booleans will be boxed into Boolean wrappers. If assertJ does not box the actual values, then you’ll need to change the instance-of check
    💯 1
    t
    j
    m
    • 4
    • 6
  • d

    df

    02/03/2019, 7:43 PM
    Hi there. I'm using reflection in order to get a methods parameter list. Unfortunately kotlin and java return different values for the same signature. a) is that on purpose b) is there a way to get the same results https://gist.github.com/dfreudenberger/d9f7e6aa7031aa106a14692223c3d1b8
    a
    • 2
    • 4
  • d

    Davide Giuseppe Farella

    02/03/2019, 8:57 PM
    How could then pass the param through
    child::childNodeBlock
    ?
    n
    • 2
    • 3
  • n

    natpryce

    02/04/2019, 8:15 AM
    Static imports in Java are not “bad”. Static mutable state can cause problems. Kotlin helps one avoid mutable state, with an emphasis on immutable data and pure functions.
    👍 2
    ☝️ 1
    a
    • 2
    • 19
  • c

    Czar

    02/04/2019, 8:49 AM
    Static imports can be bad for code readability. For example, I've had a misfortune to deal with a code base with these collection "literals" all over it:
    of(a, b, c)
    , where
    of
    was a static import of
    ImmutableList.of
    ,
    ImmutableSet.of
    etc. You always had to look at the imports to understand the code. Arguably that's a naming/misunderstood intention problem though, Google Collections' team obviously intended
    *.of
    not to be statically imported.
    n
    • 2
    • 4
  • b

    Bernhard

    02/04/2019, 8:57 AM
    so basically !!
    p
    c
    • 3
    • 8
  • d

    dimsuz

    02/04/2019, 10:49 AM
    Why is the following not allowed?
    sealed class Outer {
      data class Inner1(val value: Int) : Outer()
      data class Inner2(val value: String) : Outer()
    }
    
    typealias OuterAlias = Outer
    
    fun main() {
       val i = OuterAlias.Inner1(3) // gives an error "Unresoved reference: Inner1"
    }
    If typealias is just an alias the above should be possible I guess.
    g
    • 2
    • 2
  • d

    dave08

    02/04/2019, 11:39 AM
    What's the difference between
    find { }
    and
    firstOrNull { }
    on
    Iterable<T>
    in the stdlib?
    g
    a
    • 3
    • 7
  • b

    Bernhard

    02/04/2019, 1:52 PM
    does runBlocking create a new coroutine scope so that your jobs inside of it won’t leak on exceptions
    h
    d
    • 3
    • 2
  • g

    guppyfaced

    02/04/2019, 4:27 PM
    I have a Kotlin based question, which I've posted to stackoverflow. Help is much appreciated: https://stackoverflow.com/questions/54520330/abstract-factory-and-inheritance-kotlin
    h
    r
    • 3
    • 12
  • m

    Micah Church

    02/04/2019, 9:37 PM
    If I have specific questions about a github issue and some further clarification on what was written where would I ask such questions?
    c
    g
    • 3
    • 7
  • k

    kevinherron

    02/04/2019, 11:16 PM
    has anyone else seen this internal compiler error since upgrading to 1.3.20? https://youtrack.jetbrains.com/issue/IDEA-206642
    p
    • 2
    • 2
  • m

    Marc Knaup

    02/05/2019, 7:42 PM
    Is it possible to make a class
    open
    to its own module only but not to "the public"?
    r
    m
    n
    • 4
    • 13
  • r

    Robert

    02/05/2019, 9:27 PM
    Why does a `StringBuilder`have a method
    append
    but no method
    prepend
    , that does not make much sense to me. Anyone knows why there's none?
    r
    a
    +3
    • 6
    • 21
  • f

    frellan

    02/05/2019, 9:58 PM
    What would be the functional equivalent of this? I frown upon these MutableList things (the old java way) nowadays and always
    map
    stuff instead but I can’t figure out how to do this. I’m essentially looping over a sequence and I want to place things into three different lists, like
    partition
    but for three output collections instead of two. Is there some general function or a combination that can do this?
    val success: MutableList<String> = ArrayList()
    val failed: MutableList<String> = ArrayList()
    val ignored: MutableList<String> = ArrayList()
    
    for (string in strings) {
        val result = doStuff(string)
        when {
            result != null -> success.add(ingredient)
            other condition -> ignored.add(string)
            else -> failed.add(string)
        }
    }
    j
    r
    +3
    • 6
    • 13
  • a

    Andrew Gazelka

    02/06/2019, 12:16 AM
    is it weird to have setters in Immutable classes (which return new instances of "class")
    j
    • 2
    • 2
  • l

    Lukretsiy

    02/06/2019, 6:40 AM
    Hello, any kmongo experts here? How to insert data class object into mongodb preserving class properties naming?
    data class Lol {val Kek: String)
    Currently,
    Kek
    will be mapped to
    kek
    , but I want to preserve it as
    Kek
    y
    • 2
    • 2
  • m

    marcinmoskala

    02/06/2019, 12:01 PM
    What do you use or prefer, KtLint vs Detekt? Why?
    s
    u
    a
    • 4
    • 9
  • d

    Dias

    02/06/2019, 2:09 PM
    not sure where to post this, but when you use
    runBlocking {}
    in junit tests and you have
    assertEquals()
    as the last call (or any kotlin test assertion) it doesn't work, because tests should have
    Unit
    return type, but since
    asserEquals
    returns stuff, junit doesn't pick up the test. What can I do? I can just put
    Unit
    in the end of the call, but it does't look good
    t
    p
    k
    • 4
    • 15
  • a

    altavir

    02/06/2019, 2:21 PM
    Does anybody know, for what reason this one https://pl.kotl.in/Sk2RqPdEN does not throw compile-time error?
    t
    a
    +3
    • 6
    • 37
  • k

    karelpeeters

    02/06/2019, 10:13 PM
    You want to find
    toReplace
    in
    list
    and replace it with
    replacement
    ? Because the code doesn't require an exact match right now.
    d
    n
    • 3
    • 11
Powered by Linen
Title
k

karelpeeters

02/06/2019, 10:13 PM
You want to find
toReplace
in
list
and replace it with
replacement
? Because the code doesn't require an exact match right now.
d

Daniel

02/06/2019, 10:19 PM
Yep, this is only a very simplified example, you can be sure that
toReplace
is part of the
list
k

karelpeeters

02/06/2019, 10:21 PM
replaceInList(listOf(1,2,1,5,2), listOf(1,5,2), listOf(6))
d

Daniel

02/06/2019, 10:22 PM
The result would be listOf(1, 2, 6)
k

karelpeeters

02/06/2019, 10:23 PM
But that's not what your example code does, right?
d

Daniel

02/06/2019, 10:26 PM
I hope I got the example right, the first sublist would be
1, 2
, then
+ 6
and the last sublist, yeah would throw an index out of bounds exception, I simplified a bit too much, it seems. Thank you for your reply!
k

karelpeeters

02/06/2019, 10:27 PM
Ah I wasn't even thinking about that, the main problem is that it only looks at the first and last elements for a match. Even the size doesn't matter!
d

Daniel

02/06/2019, 10:30 PM
Maybe I am thinking wrong here, but the sizes shouln't matter. So you could go with
listOf(1,2,3,4)
replace
listOf(2,3)
with
listOf(6,7,8)
or
listOf(6)
and should end up with
listOf(1,6,7,8,4)
or
listOf(1,6,4)
so any replacement should be possible. For the
toReplace
you can be sure that it is part of the
list
k

karelpeeters

02/06/2019, 10:32 PM
Yes, but your code right now doesn't necessarily find the correct position for
toReplace
in
list
because it only checks the first and last positions. The size of
toReplace
and the matched part of
list
don't even have to match.
d

Daniel

02/06/2019, 10:54 PM
Ohhh, now I understand, sorry. In the original code the matching is done over identity. So you can be sure that the right element is found
n

nkiesel

02/07/2019, 12:43 AM
I think what you are trying to say is: "in the original code, the list does not contain any duplicate values". As soon as you have duplicates, the code might not do what you want. Consider original list is
[1,1,1,2,2,2]
and you want to replace
[1,2]
with
[3]
. I think you want
[1,1,3,2,2]
but your code would produce
[3,2,2]
instead. But even that is not good enough because
replaceInList(listOf(1,2,3,4,5), listOf(4,2), listOf(0))
would produce
[1,2,3,0,3,4,5]
and
replaceInList(listOf(1,2,3,4,5),listOf(1,5),listOf(0))
would produce
[0]
a valid solution (assuming you only want to replace the fist match) would be
fun replaceInList(list: List<Int>, toReplace: List<Int>, replacement: List<Int>): List<Int> {
    val i = list.windowed(toReplace.size).indexOfFirst { it == toReplace }
    return if (i == -1) list else list.take(i) + replacement + list.drop(i + toReplace.size)
}
View count: 4