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

    Tsvetozar Bonev

    03/12/2019, 10:22 AM
    I'm trying to use the getHeader() method of an HttpServletResponse, Intellij doesn't highlight anything but kotlin refuses to compile, claiming getHeader is unresolved. How do I fix this?
    k
    d
    • 3
    • 4
  • l

    lukaville

    03/12/2019, 11:52 AM
    Is there any way to get kotlin stdlib version at runtime? Does it have some constant with the version number?
    d
    • 2
    • 1
  • v

    Vinicius Carvalho

    03/12/2019, 3:51 PM
    runBlocking {
    foo() //foo returns a value and is being captured by the lambda
    }
    Since runBlocking needs to return void, I get a compiler error
    s
    d
    • 3
    • 4
  • a

    AndreasBackx

    03/12/2019, 6:48 PM
    I'm trying to call https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html#exceptionally-java.util.function.Function- from Kotlin using a lambda:
    exceptionally { 
        it.printStackTrace()
    }
    But it complains
    Type mismatch. Required: Void! Found: Unit
    . Shouldn't have Kotlin handled this properly or am I missing something?
    s
    g
    • 3
    • 15
  • l

    LeoColman

    03/12/2019, 10:04 PM
    The compiler is throwing a KotlinNullpointerException when trying to compile some of my classes... What should I do? x.x
    l
    • 2
    • 1
  • a

    armaxis

    03/12/2019, 11:32 PM
    Need help with Kotlin-Java interop!
    //Tasks.kt
    abstract class Foo : Callable<Unit> {   
        override fun call() { bar() }
        abstract fun bar()
    }
    
    //Manager.java
    public class FooChild extends Tasks.Foo {
        @Override public void bar() { //haha }
    }
    Class FooChild is highlighed as Error with message: 'call()' in 'Tasks.Foo' clashes with 'call()' in 'java.util.concurrent.Callable'; attempting to use incompatible return type
    d
    • 2
    • 4
  • s

    sitepodmatt

    03/13/2019, 3:04 AM
    Why did the language designers decide to use :: though, instead of just .
    s
    • 2
    • 1
  • j

    jw

    03/13/2019, 3:05 AM
    how do you make a property reference with that?
    👆 2
    s
    g
    • 3
    • 14
  • b

    bbaldino

    03/13/2019, 3:54 AM
    is there a way to conditionally invoke a nullable receiver function?
    s
    • 2
    • 8
  • p

    Paul Woitaschek

    03/13/2019, 9:20 AM
    Did someone try the new type inference? Did you encounter issues or is it stable?
    p
    a
    +3
    • 6
    • 9
  • s

    Sebastian Keller

    03/13/2019, 12:48 PM
    Hey! Is it possible to create an
    CPointer<BooleanVar>
    without the use of the nativeHeap. I would like to create one on the stack, but everythin i've tried failed. Currently im using this
    val flag = nativeHeap.alloc<BooleanVar>(); flag.usePinned { it.get().ptr /* <- CPointer<BooleanVar> */}
    i
    • 2
    • 2
  • b

    brian

    03/13/2019, 4:11 PM
    Hi, is there a password encrypting library like bcrypt
    d
    e
    +2
    • 5
    • 5
  • r

    robstoll

    03/13/2019, 8:55 PM
    is there a way to turn a type parameter with upper bound
    Any?
    into its non-nullable variant? I would like to avoid something like:
    fun <T: Any> foo(t: T?)
    s
    p
    i
    • 4
    • 33
  • h

    Horv

    03/14/2019, 8:58 AM
    Sorry, I'm not sure which channels is appropriate for this question, please redirect if there's a better place. We're building a backend webservices using Spring Boot + Kotlin and consumes different artifacts with domain models (mixed Java and Kotlin sources). I'd like to make it as easy as possible for our frontend development to consume the API and is looking into putting out a TypeScript-klient as part of the project or at least TypeScript-definitions. Has anyone done something similar and can give any pointers to good tools to use to convert Java/Kotlin-classes to TypeScript-definitions? Only thing I've found so far is https://github.com/ntrrgc/ts-generator
    t
    m
    • 3
    • 6
  • d

    david-wg2

    03/14/2019, 9:31 AM
    you're adding a void?
    e
    s
    • 3
    • 5
  • a

    Antero Duarte

    03/14/2019, 12:52 PM
    Hey, quick recommendation question. Embedded DBs? I want something simple, file based, could be key/value or more complex. Ideally it would let me serialise custom objects automatically. Nitrite+Potassium Nitrite seemed perfect, but I'm getting way too many dependency conflicts due to using apache spark in the same project (jackson is being a pain to get right in terms of versions, which breaks all the serialisation for NO2) This is basically a project that I'm converting form Scala to Kotlin. The scala version used SQLite with an ORM, which is a perfectly reasonable slution. In which case, is there an ORM you'd recommend? Sorry, this ended up being longer than I expected 😂
    d
    m
    +4
    • 7
    • 10
  • i

    Iaroslav Postovalov

    03/14/2019, 1:40 PM
    How to use
    @JvmDefault
    with Gradle?
    d
    • 2
    • 1
  • d

    darych

    03/14/2019, 2:33 PM
    Hi all. Could somebody explain why I get this message during Maven build:
    [WARNING] Runtime JAR files in the classpath should have the same version. These files were found in the classpath:
        C:/Users/somebody/.m2/repository/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.3.21/kotlin-stdlib-jdk8-1.3.21.jar (version 1.3)
        C:/Users/somebody/.m2/repository/org/jetbrains/kotlin/kotlin-stdlib/1.2.41/kotlin-stdlib-1.2.41.jar (version 1.2)
        C:/Users/somebody/.m2/repository/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.2.41/kotlin-stdlib-jdk7-1.2.41.jar (version 1.2)
    [WARNING] Some runtime JAR files in the classpath have an incompatible version. Consider removing them from the classpath
    In Maven dependencies I see only this with 1.2.41 version:
    [INFO] +- org.jetbrains.kotlin:kotlin-stdlib-jdk8:jar:1.3.21:compile
    [INFO] |  +- org.jetbrains.kotlin:kotlin-stdlib:jar:1.2.41:compile
    [INFO] |  |  \- org.jetbrains:annotations:jar:13.0:compile
    [INFO] |  \- org.jetbrains.kotlin:kotlin-stdlib-jdk7:jar:1.2.41:compile
    i
    • 2
    • 6
  • s

    Smith

    03/14/2019, 2:51 PM
    With coroutines, what is the one-to-many synchronization primitive? Basically I have a game loop, and a number of client coroutines which use select {}. When the game loop completes a cycle, I want to wake up all the clients. I could have the game loop wake up clients one by one, but I was wondering if there’s a better way.
    k
    • 2
    • 1
  • s

    Smith

    03/14/2019, 2:56 PM
    @Travis Griggs “The opt-in can be given in two ways: - either annotate the code element that uses unsigned types with the @UseExperimental(ExperimentalUnsignedTypes::class) annotation - or specify -Xuse-experimental=kotlin.ExperimentalUnsignedTypes compiler option”
    t
    k
    • 3
    • 4
  • s

    Smith

    03/14/2019, 3:14 PM
    So my earlier question boils down to - is there a one-to-many synchronization primitive that I can repeatedly select {} on? I need many coroutines waiting on a single event
    s
    • 2
    • 2
  • h

    Horv

    03/14/2019, 4:06 PM
    Hmm, anyone have experience with Kotlin, Java and Jackson with custom serializer? Mine seems to not match the types I've provided and I'm a bit baffled as to why.
    h
    • 2
    • 2
  • p

    poohbar

    03/14/2019, 4:30 PM
    @Iaroslav Postovalov wtf man. not cool
    🙂 1
    s
    • 2
    • 9
  • l

    LeoColman

    03/14/2019, 6:22 PM
    I think this was discussed before, but there's no way to inherit a companion object, right? For instance, I need that every
    Model
    class have a
    Factory
    for it
    b
    • 2
    • 4
  • x

    xenoterracide

    03/14/2019, 7:05 PM
    if I have a var lateinit, can I override the getter to provide a default later? not sure where exactly the non null check in the lateinit will be performed, it seems to be on access
    c
    d
    • 3
    • 16
  • h

    Hullaballoonatic

    03/14/2019, 7:28 PM
    Isn't there an annotation or something i can use to tell the compiler to smart-cast
    Connection
    to not null?
    private val Connection?.isUsable get() = this != null && !isClosed
    d
    • 2
    • 1
  • h

    Hasan

    03/15/2019, 7:04 AM
    Hi we are doing school object oriented project so we have to draw uml diagram.Would you recommend free uml tools?Uml stands for Unified Modeling Language.
    :google: 3
    d
    j
    r
    • 4
    • 7
  • j

    Jan

    03/15/2019, 8:16 AM
    how_come_i_can_use_java_interfaces_here_but_not_kotlin_interfaces_.kt
    m
    s
    • 3
    • 2
  • r

    Rohit Singh

    03/15/2019, 11:26 AM
    sealed class BaseViewEffect{
        object ShowView : BaseViewEffect()
    }
    
    sealed class ChildViewEffect : BaseViewEffect {
        
    }
    anyone can help me to achieve inheritance for sealed class
    g
    j
    +3
    • 6
    • 27
  • d

    David Cuesta

    03/15/2019, 1:22 PM
    Hey guys, anyone knows how can I check if a lateinit var is initialized from a function of the same class where the lateinit var resides?
    d
    d
    +3
    • 6
    • 20
Powered by Linen
Title
d

David Cuesta

03/15/2019, 1:22 PM
Hey guys, anyone knows how can I check if a lateinit var is initialized from a function of the same class where the lateinit var resides?
d

Dico

03/15/2019, 1:24 PM
That's not possible without using reflection or any other way of accessing the backing field directly. I have a workaround though which looks like this. lateinit.kt
/**
 * This function will always return null, but the compiler doesn't understand that.
 *
 * To be used instead of lateinit modifier,
 * where one wishes to check if the property was initialized,
 * or where one wishes to avoid the null check inserted in the getter.
 *
 * @throws NullPointerException if T is a concrete, that is, not generic, primitive type.
 */
@Suppress("UNCHECKED_CAST")
fun <T : Any> lateinit(): T = (nullPropInstance as Lateinit<T>).value


private class Lateinit<T : Any> {
    val value: T = run { value }
}

private val nullPropInstance = Lateinit<Any>()
d

David Cuesta

03/15/2019, 1:24 PM
Thank you @Dico
d

Dico

03/15/2019, 1:24 PM
Before:
lateinit var a: String
After:
var a: String = lateinit()
d

diesieben07

03/15/2019, 1:25 PM
::a.isInitialized
☝️ 3
w

wbertan

03/15/2019, 1:25 PM
if (::lateVar.isInitialized)
d

diesieben07

03/15/2019, 1:25 PM
does not require reflection.
d

Dico

03/15/2019, 1:25 PM
oh that's a thing 😂
f

Fredrik Larsen

03/15/2019, 1:25 PM
this::foo.isInitialized
d

Dico

03/15/2019, 1:25 PM
I feel dumb
d

David Cuesta

03/15/2019, 1:26 PM
the property that I want to check is inside a companion object and isInitialized doesn't work
d

Dico

03/15/2019, 1:26 PM
I don't recommend using that code then. You lose the exception message saying you accessed a lateinit property before it's initialized.
f

Fredrik Larsen

03/15/2019, 1:28 PM
You can make a function in your companion object that returns it then, since you can't access the backing field safely at that time.
d

diesieben07

03/15/2019, 1:28 PM
class Foo {
  companion object {
    lateinit var foo: String
    val fooInitialized get() = ::foo.isInitialized
  }
}

fun main() {
  println(Foo.fooInitialized)
}
d

David Cuesta

03/15/2019, 1:28 PM
thank you, I like that solution
what do you recommend to wait until the variable is initialized? Using just a while? or another solution?
d

diesieben07

03/15/2019, 1:29 PM
Don't use lateinit, use a property with custom setter.
Then you know exactly when it's set
d

David Cuesta

03/15/2019, 1:31 PM
The lateinit var is going to be initialized when the class is instantiated ( is initialized in the constructor) and the lateinit var resides in the companion object
s

streetsofboston

03/15/2019, 1:58 PM
My experience is that if you need to check for something like
isInitialized
, you should consider a nullable type instead and use the
?.
syntax.
d

David Cuesta

03/15/2019, 2:16 PM
yep.. I am doing that right now after some attempts
View count: 11