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

    halirutan

    03/08/2019, 6:40 AM
    Simple question: I need a set of classes that extend (in java) from a base class so that I can iterate over the set and test an object if it
    is Base
    . Something along
    val clazzes : MutableSet< ???> = ...
    and then e.g.
    clazzes.forEach { c -> if(obj is c){..}}
    . How can I do that?
    b
    d
    • 3
    • 11
  • u

    4ntoine

    03/08/2019, 12:23 PM
    Hey, guys. We’re going to reconsider our system design. Can you point some C++ vs. Kotlin comparisons, benchmarks? Probably including some project management aspects like “coding time” (amount of time required to implement the same functionality), developers costs (average salary), learning curve, etc. Thanks in advance
    c
    m
    +5
    • 8
    • 63
  • s

    Siva

    03/08/2019, 1:13 PM
    Hi friends we are looking for a opensource Kotlin/Java end to end API testing framework any recommendations would be good.
    m
    • 2
    • 3
  • m

    marcinmoskala

    03/08/2019, 4:12 PM
    There is an error in the IntelliJ template for Android & iOS MPP: Wrapper is missing. Where can I PR it?
    a
    • 2
    • 1
  • a

    amadeu01

    03/08/2019, 4:23 PM
    guys do you know something like https://github.com/krzysztofzablocki/Sourcery for kotlin?
    t
    • 2
    • 2
  • g

    Gianpaolo

    03/08/2019, 4:32 PM
    If someone is interested, entire pluralsight catalog for free just for the weekend https://learn.pluralsight.com/offers/2019/free-weekend
    😀 1
    a
    j
    • 3
    • 5
  • i

    igor.wojda

    03/08/2019, 9:58 PM
    What is the easiest way to transpose 2 dimentional list in kotlin?
    // now
    listOf(
                listOf(1, 2),
                listOf(3, 4)
            )
    
    // want
    listOf(
                listOf(3, 1),
                listOf(4, 2)
            )
    r
    h
    • 3
    • 3
  • b

    basher

    03/08/2019, 11:15 PM
    Will the Kotlin compiler optimize away / remove methods that were generated by declaring a class a
    data class
    , but ended up being unused?
    🇳🇴 1
    j
    g
    • 3
    • 7
  • l

    LeoColman

    03/09/2019, 4:11 AM
    Some of the issues is "I want this function to be suspendable, but I don't to mark it suspend" -> Inline it so it will use whichever context it is in.
    s
    d
    l
    • 4
    • 16
  • z

    zsmb

    03/09/2019, 5:55 PM
    Is there a way to mark something with
    @Deprecated(level = ERROR, ...)
    , or even
    HIDDEN
    , but still use it in my own code somehow? Seems like I can only suppress the
    WARNING
    level of deprecation.
    t
    d
    • 3
    • 5
  • c

    Cihan Soylular

    03/09/2019, 6:44 PM
    Hello guys i’m trying make a exchange application. I will take informations on json file. What is thr best library(s) for working on json? Thanks
    c
    g
    +2
    • 5
    • 5
  • g

    groostav

    03/09/2019, 10:11 PM
    Hey guys, I found a problem with an inspection and I've found the commit that introduced this inspection, but the problem I think is more with how intelliJ is able to implicitly tag some methods with annotations. In my case,
    ProcessBuilder.start()
    is somehow intrinsically labeled (annotated?) as Blocking (
    @Blocking
    ?), which is simply wrong. I have no idea how to find out where this metadata about ProcessBuilder.start as being a blocking method is coming from, but I'd like to log it as a bug. How might I do that?
    s
    e
    • 3
    • 6
  • k

    kieran

    03/10/2019, 10:23 AM
    Can someone please point me to an example of a minimal gradle build file that will allow building a shared library for Android and iOS targets, but without including a full blown sub project for each. There won't be any platform specific code. It will literally be the same code compiled for both targets. I've been banging my head against this for days using IDEA templates and official samples as starting points but I'm getting nowhere.
    r
    g
    r
    • 4
    • 13
  • i

    iex

    03/10/2019, 12:04 PM
    Hello! Is there a way to somehow assign variables in the left side expression of a
    when
    clause, to be used in the right side?
    when {
        toFoo(line) != null -> elements.add(toFoo(line)) // reuse left side result instead?
    }
    p
    s
    • 3
    • 11
  • o

    olegstepanov

    03/10/2019, 1:53 PM
    Hi Can anyone explain why this won't compile:
    val String.prop
        get() = object : Supplier<Int> {
            override fun get(): Int = this@prop.toInt()
        }
    even though completion actually suggests this@prop?
    s
    • 2
    • 6
  • k

    karelpeeters

    03/10/2019, 7:51 PM
    You can look into generating the code, eg. using KotlinPoet: https://github.com/square/kotlinpoet. It all still has to be compile-time of course.
    g
    • 2
    • 1
  • n

    Nicole

    03/10/2019, 8:09 PM
    I think the answer is no, but is there anything like a Union for Kotlin outside of Kotlin native?
    r
    s
    • 3
    • 3
  • l

    LeoColman

    03/11/2019, 2:23 AM
    Why can't this work:
    fun bar(fn: suspend () -> Unit ) { }
    fun far() {  
       val fn = {} // () -> Unit
       bar(fn)
    }
    While I can easily do:
    fun far() {
      val fn = {}
      bar {  fn() }
    }
    s
    • 2
    • 8
  • s

    sitepodmatt

    03/11/2019, 2:42 AM
    inline fun bar(fn: () -> Unit ) { 
    fn()
    }
    
    class InlineSuspendExperiments : StringSpec({
    
        "test experimenting" {
            runBlocking {
                bar {
                    delay(100)
                }
            }
        }
    })
    l
    • 2
    • 6
  • r

    reik.schatz

    03/11/2019, 8:34 AM
    is there a way to generate a sequence but keep the invocation count internal (i.e. not having a mutable
    count
    as in: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.sequences/generate-sequence.html)
    p
    g
    s
    • 4
    • 4
  • m

    mahdi_perfect

    03/11/2019, 11:58 AM
    Hi. I've just made a simple markdown DSL, maybe it helps you somewhere. github.com/mahdi13/markdownk.git Also, I would like to know your opinion about this kind of projects, I mean 'converting (almost) everything to kotlin'. Things like android manifest, resources... It could make us reach 100% kotlin projects. Do you think its good? Or wasting time...?
    t
    s
    +3
    • 6
    • 15
  • g

    Giorgio Antonioli

    03/11/2019, 1:44 PM
    Can someone help me with the problem I posted above?
    p
    • 2
    • 2
  • l

    LeoColman

    03/11/2019, 2:15 PM
    Hey guys, is there anyway to permute all elements in an array with all elements in the other? I'm trying to simplify this:
    val foo = firstArr.flatMap { a -> secondArr.map { b -> a to b } }
    s
    k
    • 3
    • 7
  • m

    morbidmerve

    03/11/2019, 3:21 PM
    Hey guys, I’m trying to figure out how to deserialize/parse an ArrayList from n json string…where the ArrayList object has a generic element type, say
    ArrayList<UserActivity>
    is the type of the list. anyone have any suggestions on standard practice for ArrayLists? lets say the arrayListString looks something like
    {{...somefields...}, {...somefields...}, ... }
    where
    somefields
    are the fields of the UserActivity type.
    d
    • 2
    • 2
  • a

    AndreasBackx

    03/11/2019, 3:27 PM
    I'm writing a very simple socket server that acts as an HTTP2 client and does some measurements on HTTP2 Server Push for some research as the existing codebase does not have support for a proper HTTP2 client. So I've got an HTTP2 client aspect and socket server aspect in my code. The server listens for some JSON to be sent, parses it, and sends it on a socket that the client is listening on in a coroutine. However the time between sending and receiving the data can be very inconsistent. Because the measurements have to be quite accurate and consistent, I need this to be consistent. (Let's ignore the other speed improvements that could be made for now.) The delay can be almost non existent (1ms or so), 500ms, or almost 2 seconds, see below for some simple logs. Below is also the unparsed JSON data that is sent (it is parsed to data classes and sent over the channel as a List), it is definitely not a large amount of data. What could be the reason for this inconsistency?
    [2019-03-11 16:13:33.075] [INFO] Sending items... 
    [2019-03-11 16:13:35.746] [INFO] Items received...
    [{"segment": 1, "tile": -1, "quality": 0}, {"segment": 1, "tile": -2, "quality": 0}, {"segment": 1, "tile": -3, "quality": 0}, {"segment": 1, "tile": -4, "quality": 0}, {"segment": 1, "tile": -5, "quality": 0}, {"segment": 1, "tile": -6, "quality": 0}, {"segment": 1, "tile": -7, "quality": 0}, {"segment": 1, "tile": -8, "quality": 0}, {"segment": 1, "tile": -9, "quality": 0}, {"segment": 1, "tile": -10, "quality": 0}, {"segment": 1, "tile": -11, "quality": 0}, {"segment": 1, "tile": -12, "quality": 0}, {"segment": 1, "tile": -13, "quality": 0}, {"segment": 1, "tile": -14, "quality": 0}, {"segment": 1, "tile": -15, "quality": 0}, {"segment": 1, "tile": -16, "quality": 0}, {"segment": 1, "tile": -17, "quality": 0}, {"segment": 1, "tile": 1, "quality": 1}, {"segment": 1, "tile": 13, "quality": 1}, {"segment": 1, "tile": 10, "quality": 1}, {"segment": 1, "tile": 9, "quality": 1}, {"segment": 1, "tile": 6, "quality": 1}, {"segment": 1, "tile": 17, "quality": 1}, {"segment": 1, "tile": 14, "quality": 1}, {"segment": 1, "tile": 5, "quality": 1}, {"segment": 1, "tile": 2, "quality": 1}, {"segment": 1, "tile": 16, "quality": 1}, {"segment": 1, "tile": 15, "quality": 1}, {"segment": 1, "tile": 4, "quality": 1}, {"segment": 1, "tile": 3, "quality": 1}, {"segment": 1, "tile": 12, "quality": 1}, {"segment": 1, "tile": 11, "quality": 1}, {"segment": 1, "tile": 8, "quality": 1}, {"segment": 1, "tile": 7, "quality": 1}]
    k
    d
    • 3
    • 18
  • d

    David Cuesta

    03/11/2019, 5:56 PM
    Hey Guys. I am having a problem with coroutines. I have the next:
    GlobalScope.launch {
                    CoroutineScope(a.context).launch {
                    val b = c.await()
                    store(b)
                    println("Hello")
                }.join()
            }
    where
    a
    is an object which contains a Coroutine context and
    c
    is a deferred value obtained from a calling a suspend function. It never prints
    Hello
    because there is a NullPointerException when tries to store
    a
    . Anyone knows why is it happening?? My target is to wait until the deferred value a is completed and then store it using the
    store
    function, which is not a suspend function.
    l
    g
    d
    • 4
    • 11
  • r

    Ruckus

    03/11/2019, 6:56 PM
    In the docs for
    kotlin.math.ceil()
    it says it returns:
    the smallest double value that is greater than the given value
    x
    and is a mathematical integer
    Shouldn't it say:
    the smallest double value that is greater than or equal to the given value
    x
    and is a mathematical integer
    (similar with
    floor
    , but obviously the other way around)
    EDIT: The documentation is actually correct, I just need to learn to read (thanks @karelpeeters)
    k
    i
    • 3
    • 5
  • v

    vkuznetsov

    03/11/2019, 6:56 PM
    Just curious, is there a simple way to insert X character at every Y positions of the string? Currently doing it by mapIndexed and joinToString, but looking into more accurate solution. Thought about chunked and joining, but I don't want to have X at the end of the String. Example string is 123456 to become 12:34:56
    h
    • 2
    • 1
  • b

    bbaldino

    03/11/2019, 7:50 PM
    is there a more efficient way to convert 2 bytes to a short in kotlin? i'm worried this is creating a bunch of intermediary types and will be a waste:
    fun ByteArray.getShort(byteIndex: Int): Short {
        val b1 = (get(byteIndex) and 0xFF.toByte()).toInt()
        val b2 = (get(byteIndex + 1) and 0xFF.toByte()).toInt()
    
        return ((b1 shl 8 ) or (b2)).toShort()
    }
    i could keep static instances of the masks (
    0xFF.toByte()
    ) but i still have to create the Ints which seems like a waste.
    k
    • 2
    • 17
  • b

    bbaldino

    03/11/2019, 10:16 PM
    is it possible to define a member to look like it is hidden within another? that is, could i define a class like:
    class Foo {
      var baz: Int
    }
    such that callers accessed
    baz
    by doing
    Foo.bar.baz
    ?
    s
    • 2
    • 9
Powered by Linen
Title
b

bbaldino

03/11/2019, 10:16 PM
is it possible to define a member to look like it is hidden within another? that is, could i define a class like:
class Foo {
  var baz: Int
}
such that callers accessed
baz
by doing
Foo.bar.baz
?
s

streetsofboston

03/11/2019, 10:21 PM
Not sure why you’d want this, but this would work:
class Foo {
  var baz: Int
}

val Foo.bar get() = this
Now, you can call
val value: Int = myFoo.bar.baz
b

bbaldino

03/11/2019, 10:23 PM
ah that's pretty slick! don't suppose there'd be a way to do it which would make
baz
only accessible through
bar
?
the use case is that i've got some properties in Foo that logically would be nested into an inner member, but Foo needs to potentially take action when they're modified (and access Foo data), so was thinking about this as a possible method
and an inner class doesn't feel like the right model, conceptually
s

streetsofboston

03/11/2019, 10:25 PM
Then an inner class would be good for this. Why don’t you think it’s right to use an inner class?
b

bbaldino

03/11/2019, 10:29 PM
it feels like this class should be more 'independent' than existing as an inner class, but maybe i'll rethink that.
s

streetsofboston

03/11/2019, 10:29 PM
interface IFoo {
    var baz : Int
}

class Foo {
    private var baz: Int = 8

    val bar : IFoo = object : IFoo {
        override var baz: Int
            get() = this@Foo.baz
            set(value) { this@Foo.baz = value }
    }
}

fun main() {
    Foo().bar.baz
}
➕ 1
b

bbaldino

03/11/2019, 10:30 PM
ah that's i think what i was looking for. thanks! i'll weigh that vs the inner class
s

streetsofboston

03/11/2019, 10:42 PM
Or weirder 🙂
interface InnerFoo {
    val bar : Foo
}

class Foo private constructor() {
    companion object {
        operator fun invoke() : InnerFoo = object : InnerFoo {
            override val bar = Foo()
        }
    }

    private var hiddenProp: Int = 0

    var baz : Int = 0
}

fun main() {
    val foo = Foo()
    foo.bar.baz
    foo.bar.baz = 29
}
😮 1
View count: 4