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

    pepe

    08/21/2019, 11:04 AM
    hey! I'm trying to write a generic function to set a default value on a nullable property of an immutable object and I have two obstacles: - can I constraint a type so it is a data class (implements a copy constructor)? - can I address the right property in the copy constructor using the KProperty? This is what I have so far:
    fun <T: ???, R> T.setDefault(prop: KProperty1<T, R>, defaultValue: R): T =
            prop.get(this)?.let { this } ?: copy(??? = defaultValue)
    P.S. maybe this I'm trying to do is stupid/not worth it, feel free to point it out
    m
    r
    • 3
    • 4
  • r

    ribesg

    08/21/2019, 12:51 PM
    Is there a documentation about which
    stdlib
    dependency does what? For example, should I use
    stdlib-jdk8
    for an Android lib/app?
    m
    l
    m
    • 4
    • 7
  • j

    javaru

    08/21/2019, 3:08 PM
    What's the best way to go from a
    Stream<T>
    to a typed array of
    Array<T>
    ? I need to convert a
    Map<String,String>
    to an
    Array<Pair<String,String>>
    . I've done this as follows:
    val array: Array<Pair<String, String>> = myMap.entries.stream().map {
            Pair(it.key, it.value)
        }.toList().toTypedArray()
    Is that the most efficient way? It seems like converting to a
    List
    first and then to the (typed)
    Array
    has an extra step. But I'm not seeing any other way to go from the
    Stream<Pair<String,String>>
    coming out of the
    map{}
    function to a typed
    Array<Pair<String,String>>
    (since
    Stream.toArray()
    would give me an untyped Array of
    Array<Any>
    ).
    i
    • 2
    • 4
  • c

    changd

    08/21/2019, 9:26 PM
    i'm having some java interop issues with
    @Throws
    The API
    interface StormcrowBase {
       @get:Throws(Exception::class)
       val featureNames: ArrayList<String>
    }
    Java call site
    try {
                    features = stormcrowBase.getFeatureNames();
                } catch (Exception e) {
                    e.printStackTrace();
                }
    The error I'm getting is
    Exception is never thrown in the corresponding block
    . Any ideas?
    l
    s
    • 3
    • 24
  • l

    Leoboyean

    08/21/2019, 10:31 PM
    Hello everyone, Someone can tell me wich is the best way to create a singleton, I had read something about "by lazy" but also another property like by DelegatesExt.notNullSingleValue()
    m
    l
    +2
    • 5
    • 5
  • p

    Paul N

    08/22/2019, 8:53 AM
    If I want to get something out of an http session, it can be either null, or of a particular type, which I have to cast to. So to cope with this I've coded something like this:
    val tokenMap = (session.getAttribute(sessionAttributeName)   ?: mutableMapOf<String, AgedToken>()) as MutableMap<String, AgedToken>
    Which means I do get a compiler warning "Warning:(39, 55) Kotlin: Unchecked cast: Any to MutableMap<String, AgedToken>" Is there any way I can do this better and get rid of the warning ?
    s
    k
    • 3
    • 5
  • f

    Fredrik Larsen

    08/22/2019, 10:30 AM
    Is anyone here successfully generating kotlin data classes from json schema in a pipeline? i.e. not using an android plugin
    a
    t
    • 3
    • 7
  • s

    Sam Garfinkel

    08/22/2019, 2:47 PM
    Can cast operators be overloaded to support safe casting of known types (conditionally)? I have a lot of factories for wrapper types (do some additional validation on the input, and return
    Type?
    ), but it would be kind of cool to replace those with
    as?
    directly.
    d
    l
    • 3
    • 5
  • j

    jmfayard

    08/22/2019, 4:13 PM
    https://github.com/thepracticaldev/dev.to/pull/3801
    m
    • 2
    • 1
  • i

    Icaro Temponi

    08/22/2019, 4:52 PM
    Oh boy, update is coming, anxiously waiting the changelog and announcement.
    b
    l
    • 3
    • 8
  • g

    gaetan

    08/22/2019, 6:11 PM
    My Intellij is already installing it. 😅
    e
    i
    • 3
    • 2
  • r

    Robert Menke

    08/22/2019, 6:13 PM
    Is there a changelog for Kotlin 1.3.5? Curious if the new type inference engine is used in the compiler now
    b
    i
    +2
    • 5
    • 10
  • r

    Ruckus

    08/22/2019, 7:46 PM
    Working on an improved Kotlin-to-Java converter.
    This should say "Java-to-Kotlin". Hopefully someone from JB can fix that.
    b
    j
    +2
    • 5
    • 4
  • x

    xenoterracide

    08/22/2019, 8:25 PM
    I’m implementing this https://www.rabbitmq.com/passwords.html#computing-password-hash, how can I generate this as a byte array in kotlin?
    908D C60A
    note: I don’t mean generate a salt I mean literally these bytes, I’m trying to test my algorithm and see if I get the same base64 encoded string
    s
    m
    • 3
    • 29
  • c

    christiangruber

    08/22/2019, 11:40 PM
    I'm really struggling with something - I'm working on the bazel rules, but as of 1.3.40, if you pull in any version of kotlin after 1.3.31, code that compiles perfectly well under the existing rules breaks on "internal". But I cannot find, for the life of me, any changelog information relating to the compiler enforcing anew, or more strictly, module-membership. Did something change between 1.3.31 and 1.3.40 that caused this increased strictness? It's literally a matter of same code, same build rules and infrastructure, swap out which version of kotlinc.zip it uses, and it builds or doesn't, if it's at or post 1.3.31 respectively. Any ideas? Any changes that didn't make it into the changelog, but which anyone knows about?
    s
    • 2
    • 3
  • s

    Slackbot

    08/23/2019, 6:49 AM
    This message was deleted.
    d
    j
    • 3
    • 5
  • a

    Alowaniak

    08/23/2019, 7:01 AM
    Do you need the reified though? Can't you get the class from the instance at runtime like
    when(defaultValue::class)
    or something like that
    d
    • 2
    • 2
  • t

    tateisu

    08/23/2019, 7:13 AM
    Is there a way to use kotlin scripting engine from android that has no javax.scripting ?
    r
    • 2
    • 1
  • a

    arild

    08/23/2019, 10:01 AM
    Are there any cases where a
    val
    might be
    const
    but it is useful not to make it
    const
    ?
    ➕ 1
    d
    h
    d
    • 4
    • 6
  • l

    LastExceed

    08/23/2019, 12:11 PM
    if (x != null) {
        y = x
    }
    is there a shorter way to do this? I recall there being some way by using the
    ?
    operator but im not sure if that was in kotlin or another language
    m
    m
    +3
    • 6
    • 44
  • c

    cubesky

    08/23/2019, 3:38 PM
    Unfortunately OpenCV only has full access with c++ header, is there any solution can I access c++ header with cinterops to generate klib?
    m
    g
    • 3
    • 4
  • m

    Marko Mitic

    08/23/2019, 4:02 PM
    Just found that
    internal
    kotlin classes are
    public
    to java code. How to write kotlin library without exposing all classes to java?
    ➕ 1
    c
    a
    • 3
    • 3
  • s

    Saiedmomen

    08/23/2019, 6:38 PM
    Can someone please tell me how I can see declarations of reflection types like
    KFunction
    and
    KSuspendFunction1
    ? I have imported imported
    kotlin-reflect
    but I have to manually import them and IDE inspection tools like go to declaration and search in path can't find their declaration
    m
    s
    • 3
    • 12
  • l

    Leon K

    08/23/2019, 7:35 PM
    is there any work on a Kotlin interpreter (for faster repl, better scripting support, etc)
    r
    • 2
    • 6
  • d

    Dennis Schröder

    08/24/2019, 10:41 AM
    Anybody also wondering about this topic? When does a when expression in kotlin need to be exhaustive and when does it not? https://github.com/Dobiasd/articles/blob/master/when_does_a_when_expression_in_kotlin_need_to_be_exhaustive_and_when_does_it_not.md
    k
    • 2
    • 1
  • p

    pavi2410

    08/24/2019, 4:46 PM
    words is CharSequence
    returns
    false
    . Isn't it wierd?
    s
    k
    +3
    • 6
    • 29
  • p

    pavi2410

    08/24/2019, 5:28 PM
    The different order of the interfaces {Appendable, CharSequence} doesn't group the compilation targets together.
    • 1
    • 1
  • p

    pavi2410

    08/24/2019, 7:19 PM
    Can anyone please help me with this? I am trying to emulate
    readLine
    function from stdlib which yields each line in the multiline text block. This is supposed to act just like
    readLine
    but reads from a fixed string instead of stdin. Here is how I'm trying to make it work: https://pl.kotl.in/f-ayPvSC-
    k
    k
    h
    • 4
    • 12
  • j

    jmfayard

    08/25/2019, 7:58 AM
    Given a DSL that is defined by an external project (Gradle in my case), is it possible to define an extension function
    fun String.someExtensionFunction() = "Hello World"
    that would be usable only from the right extension block?
    dependencies { // this is Dependencies
        ....
    }
    That seems impossible because it would require to define the function having two receivers,
    Dependencies
    and
    String
    d
    k
    • 3
    • 4
  • l

    LastExceed

    08/25/2019, 8:04 AM
    what is the purpose of
    object
    ? isn't it basically same as top level members in a separate package?
    j
    k
    • 3
    • 4
Powered by Linen
Title
l

LastExceed

08/25/2019, 8:04 AM
what is the purpose of
object
? isn't it basically same as top level members in a separate package?
j

jmfayard

08/25/2019, 8:07 AM
object Elvis
defines a singleton (there is only one Elvis) Not the same because after imports they will look like
Elvis.GREATEST_HITS
and
GREATEST_HITS
. The former is better IMHO
l

LastExceed

08/25/2019, 8:09 AM
so basically the purpose is just structure (where to find a function) and accessibility (public/private/ect) right?
k

karelpeeters

08/25/2019, 8:22 AM
And they can implement interfaces, I also use them for "do nothing" interface implementations.
For example the stdlib has the empty list, set and iterator as objects I think.
View count: 1