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
stdlib
  • a

    aimozg

    02/07/2017, 6:43 PM
    Empty collections behave that way for
    set.all { it.test }
    to be equivalent to
    !set.any { !it.test }
    in all cases. Found more information here: https://en.wikipedia.org/wiki/Vacuous_truth and http://math.stackexchange.com/questions/202452/why-is-predicate-all-as-in-allset-true-if-the-set-is-empty
    👍 4
    • 1
    • 1
  • m

    marcinmoskala

    02/17/2017, 6:28 PM
    Hey, I found out that this:
    var columns = listOf<MutableList<EventRect>>()
    for (eventRect in collisionGroup) {
        val properColumn = columns.firstOrNull { column -> !isEventsCollide(eventRect.event, column[column.size - 1].event) }
        if(properColumn != null) {
            properColumn.add(eventRect)
        } else {
            columns = columns.plusElement(mutableListOf(eventRect))
        }
    }
    is pretty common pattern. Like groupBy, but being in the group depend on other elements in this group. Like elements that are close enough to themself
    v
    • 2
    • 8
  • a

    azabost

    02/22/2017, 10:27 AM
    I have a class
    class EnumStringConverter<E extends Enum> implements Converter<E, String>
    which I must provide for this annotation:
    public @interface Convert {
        Class<? extends Converter> value();
    }
    When I do it like this:
    @get:Convert(EnumStringConverter::class)
    I get:
    Error:(95, 19) error: constructor EnumStringConverter in class EnumStringConverter<E> cannot be applied to given types;
    required: Class
    found: no arguments
    reason: actual and formal argument lists differ in length
    where E is a type-variable:
    E extends Enum declared in class EnumStringConverter
    And I don't know how to properly provide the type. Could you help please?
    i
    • 2
    • 2
  • a

    azabost

    02/22/2017, 3:18 PM
    I was trying to apply the mentioned annotation in an interface, not a class. Do you think it was causing the problems?
    i
    • 2
    • 5
  • e

    evanchooly

    02/28/2017, 6:48 PM
    i’m going to bang those out tonight!
    👍 1
    💪 1
    m
    • 2
    • 1
  • e

    elizarov

    03/05/2017, 7:22 AM
    Please, be careful what you wish for. We don't want Kotlin compiler (which is now written mostly in Kotlin and extensively uses Kotlin stdlib) to become slower than it is now, don't we? We actually plan to improve upon its performance.
    👍 1
    g
    • 2
    • 4
  • e

    elizarov

    03/06/2017, 8:30 AM
    Lenses if a functional solution to this problem. No, in imperative language you can do better. Just like with collections. How would you modify an an immutable collection efficiently? Imagine something like this:
    val list = … // some immutable list here
    val updated = list.mutate { 
       add(”…”) // efficiently perform a series of mutating operations
       add(”…”)
    }
    where
    mutate
    is a helper function that “opens” and efficiently rebuilds immutable structure:
    fun <T> List<T>.mutate(block: MutableList<T>.() -> Unit): List<T>
    You should have a similar API for immutable data classes:
    val person = Person(first = “John”, last = “Smith”) // immutable person object with lots of props
    val updated = person.mutate { 
       age = 31
       occuptation = “Manager”
    }
    It is considerably more readable than the solution with lenses.
    i
    • 2
    • 2
  • e

    elizarov

    03/06/2017, 10:15 AM
    There is not much difference in flexibility. It is just that updating nested structures with lenses becomes an exercise in following chains of maps. Moreover, if you are using lenses in Haskel-like (pure) language, then you get a benefit of do-notation and advanced optimizations over sequences of lazy operations. If we were to naively rewrite Monad-style lenses approach from Haskell to Kotlin, it would be quite verbose, because in Kotlin you’ll have to do
    ::
    to refer to functions and properties and you don’t have a do notation, and it would have been very inefficient, because you don’t have an advantage of purity to optimize back-to-back updates into one. However, Kotlin is an imperative language, so you should not be needing a do notation to start with, and you can directly express a sequence of updates to the the same object, without the need to rely on the smart optimizer to make it efficient.
    m
    • 2
    • 1
  • g

    groostav

    03/06/2017, 7:18 PM
    @elizarov the
    copy
    function is effectively a persister (read: local factory) function to make data classes persistent isn't it?
    data class Thingy(val x: Double, val y: Int)
    
    val first = Thingy(1.0, 42)
    val second = first.copy(x = 2.0)
    you dont get any sharing but you do get persistence.
    e
    • 2
    • 1
  • b

    benleggiero

    03/07/2017, 4:20 PM
    do they not already?
    r
    e
    • 3
    • 4
  • t

    treelzebub

    03/15/2017, 8:15 PM
    is anyone else getting spurious compiler errors when using
    internal typealias
    ?
    Error:(107, 91) Cannot access 'ValidationErrors': it is internal in ‘[module]’
    usages are all also in
    [module]
    i
    • 2
    • 6
  • k

    kirillrakhman

    03/24/2017, 10:38 AM
    sure, just make sure your collection is actually mutable
    m
    • 2
    • 1
  • b

    beholder

    03/24/2017, 12:17 PM
    Did I miss something or there is no function for mapping array to array? (there is only array to list)
    m
    • 2
    • 2
  • a

    Andreas Sinz

    03/25/2017, 4:50 PM
    @mg6maciej https://kotlinlang.org/docs/reference/whatsnew11.html#array-like-list-instantiation-functions
    m
    b
    • 3
    • 2
  • l

    louiscad

    03/28/2017, 11:50 AM
    @mg6maciej
    val newList = list.minusIndex(i)
    👍 1
    m
    • 2
    • 11
  • m

    mg6maciej

    03/28/2017, 12:04 PM
    I wonder if Kotlin's persistent collections have these missing APIs.
    i
    • 2
    • 8
  • m

    mg6maciej

    03/30/2017, 9:52 AM
    Is there a function that uses
    IndexedValue
    or do I just call it
    .mapIndexed(::IndexedValue)
    ?
    k
    • 2
    • 1
  • o

    orangy

    03/31/2017, 1:09 PM
    Beware that
    subList
    is a view. If original list changes, then sublist also changes.
    m
    • 2
    • 3
  • i

    ilya.gorbunov

    04/03/2017, 2:52 PM
    @mg6maciej AFAIK, an internal typealias in a public signature should be replaced with its public substitution, but that feature may be not working.
    m
    • 2
    • 2
  • c

    cedric

    04/03/2017, 8:58 PM
    @orangy The following code throws, is this the expected behavior? It looks like
    copyRecursively
    is changing the target from a directory to a file:
    fun main(args: Array<String>) {
        val from = File("/tmp/foo").apply { writeText("I'm a file") }
        val to = File("/tmp/to").apply {
            deleteRecursively()
            mkdirs()
        }
        from.copyRecursively(to, overwrite = true)
        if (! to.isDirectory) {
            throw AssertionError("Should be a directory")
        }
    }
    o
    • 2
    • 2
  • m

    miha-x64

    04/06/2017, 7:47 PM
    Some cool things such as
    Quadruple<A, B, C, D>
    ,
    Option<T>
    ,
    Either<L, R>
    ,
    fun Quadruple<*, *, *, *>.sendIntoOuterSpace()
    and many others could be implemented... outside of stdlib. 🙃 @benleggiero why do you think Triple should have all that Pair has? For example, I ignore existence of both in most cases 😒imple_smile: @mbstavola can you show any use-cases? I just wonder why do you need this.
    b
    • 2
    • 1
  • s

    sreich

    04/06/2017, 8:28 PM
    almost never uses pair, it's usually much better to make a descriptive class
    👍 2
    b
    • 2
    • 1
  • m

    mg6maciej

    04/07/2017, 7:04 AM
    How would you call a function that returns all possible sublists of a list? e.g. for
    listOf(A, B)
    =>
    listOf(emptyList(), listOf(A), listOf(B), listOf(A, B))
    v
    m
    m
    • 4
    • 10
  • m

    marcinmoskala

    04/09/2017, 9:56 AM
    IMHO 1)
    MapWithDefault
    2)
    MapWithDefault
    3)
    MapWithDefault
    with default from second
    v
    l
    • 3
    • 5
  • d

    Dmitry Kandalov

    04/12/2017, 9:45 PM
    Do you think it might be useful to have something like
    fun Throwable.stackTrace(): String
    in stdlib? (e.g. https://github.com/JetBrains/kotlin/blob/0d1c803f71667ded0b313491d9e0c50511d63cbf/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/incremental/jvmUtils.kt#L6-L6)
    i
    • 2
    • 3
  • e

    enleur

    04/21/2017, 11:56 AM
    how can I make from
    List<Pair<String, Double?>>
    Map<String,Double>
    pairs.filter { it.second != null }.toMap()
    returns
    Map<String, Double?>
    b
    • 2
    • 1
  • l

    louiscad

    04/22/2017, 7:51 PM
    My
    MutableLazy
    implementation is here: https://gist.github.com/LouisCAD/9b4ef46c12f1f6fe9b45658a8eff194c
    👍 1
    i
    • 2
    • 4
  • m

    mg6maciej

    04/23/2017, 7:40 PM
    Shouldn't
    public operator fun <T> Iterable<T>.minus(element: T): List<T>
    be defined with element as nullable?
    public operator fun <T> Iterable<T>.minus(element: T?): List<T>
    I.e. if you call
    listOf<String>(...) - null as String?
    it would just return the same
    List<String>
    and not
    List<String?>
    as it currently does.
    ➕ 3
    s
    i
    • 3
    • 25
  • m

    mg6maciej

    04/23/2017, 10:20 PM
    Why aren't plus/minus operators defined on
    Array<T>
    ?
    i
    b
    v
    • 4
    • 7
  • i

    ilya.gorbunov

    04/24/2017, 3:23 PM
    https://kotlinlang.slack.com/archives/C0B8Q383C/p1493038673085550?thread_ts=1492976413.401231&amp;cid=C0B8Q383C By the way, have you tried this one? https://github.com/Kotlin/kotlinx.collections.immutable It might be not as good in terms of performance, since right now we're more interested in its API convenience feedback.
    m
    • 2
    • 12
Powered by Linen
Title
i

ilya.gorbunov

04/24/2017, 3:23 PM
https://kotlinlang.slack.com/archives/C0B8Q383C/p1493038673085550?thread_ts=1492976413.401231&amp;cid=C0B8Q383C By the way, have you tried this one? https://github.com/Kotlin/kotlinx.collections.immutable It might be not as good in terms of performance, since right now we're more interested in its API convenience feedback.
m

mg6maciej

04/24/2017, 3:25 PM
Yes, I tried to replace my
List
usage with this, but the API is not convinient. I even told you a while ago what issues I had 🙂
I don't think reusing
kotlin.List
will work. It needs to be a separate
List
interface like this one: https://github.com/vavr-io/vavr/blob/master/vavr/src/main/java/io/vavr/collection/List.java
i

ilya.gorbunov

04/24/2017, 3:28 PM
I've interested to hear why it won't work. Do you have specific considerations against?
m

mg6maciej

04/24/2017, 3:32 PM
Well, maybe it "might" work, but you need to provide implementations for all functions that are there in the stdlib for given supertype, including extension functions.
I was trying to replace list with
ImmutableList
and then was hitting things like
take
or
drop
not defined on this interface.
But the code there was compiling, just returning ordinary
List
.
So I'd rather not put
List
as superinterface for
ImmutableList
and then users would clearly see what's possible and what's not.
i

ilya.gorbunov

04/24/2017, 3:39 PM
That's a valid concern, I've stumbled upon it too, see the issue https://github.com/Kotlin/kotlinx.collections.immutable/issues/10 Though currently it doesn't look entirely impassable, I believe we can find some solution if stdlib operators could get know something about immutable collections.
m

mg6maciej

04/24/2017, 3:43 PM
For me
take
on
ImmutableList
simply needs to return
ImmutableList
. There is no other option to connect it all with
data class
copy and friends.
I don't want casts or
toImmutableList()
after every such call.
Gotta go. Always happy to give me more feedback on immutable stuff.
I have a lot of code that does
this.copy(someList = this.someList + elem)
or
val (six, rest) = this.someList.splitAt(6); this.copy(someList = six)
, so all functions on
ImmutableList
should return the same type.
View count: 2