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

    Hullaballoonatic

    06/01/2019, 10:08 PM
    Also it's a shame that you cannot, because that could save a lot of copy+pasted code
    k
    t
    • 3
    • 4
  • h

    Hullaballoonatic

    06/02/2019, 5:43 PM
    why can't functions defined as such:
    fun foo() = true
    be referenced as such:
    val bar: () -> Boolean = foo
    ? or conversely:
    interface Foo {
        val bar: () -> Boolean
    }
    
    object Bar : Foo {
        override fun bar() = true // nope. why not?
        override val bar = { true } // yep
    }
    d
    a
    • 3
    • 9
  • f

    Florian

    06/02/2019, 9:20 PM
    What's the deal with REPL? Is it really that useful?
    h
    a
    +5
    • 8
    • 11
  • s

    sjthn

    06/03/2019, 5:59 PM
    Why can't I specify return type of a function as Result. Something like this:
    fun fetchProducts(): Result<List<Product>> { }
    IDE is erroring "kotlin.Result cannot be used as a return type"
    t
    t
    • 3
    • 3
  • h

    Hullaballoonatic

    06/03/2019, 9:00 PM
    is there a shorthand or an access modifier that i'm not aware of for the following:
    var foo = 1
        private set(v) {
            field = v
        }
    s
    a
    • 3
    • 4
  • v

    Vague

    06/04/2019, 1:47 PM
    What's the difference between using a
    Companion Object
    and using an
    Object Declaration
    ?
    s
    e
    • 3
    • 10
  • k

    karelpeeters

    06/05/2019, 8:43 AM
    There's no real solution to this problem unfortunately. As a workaround you can do something like
    v.row(j) *= -1
    which will pick the correct overload.
    ➕ 1
    d
    • 2
    • 2
  • k

    karelpeeters

    06/05/2019, 5:39 PM
    Create a field
    cols
    with an
    operator fun set
    and you can do
    M.cols[3] = v
    .
    h
    • 2
    • 16
  • h

    Hullaballoonatic

    06/05/2019, 7:53 PM
    Is there any reason I don't know of why the following would be inferior to a standard class and constructors? (it is superior in flexibility)
    interface Foo {
        val a: Int
        val b: Int
        fun sum() = a + b
    }
    
    fun Foo(a: Int, b: Int) = object : Foo {
        override val a = a
        override val b = b
    }
    is it more expensive to instantiate object literals than class instances?
    r
    • 2
    • 8
  • h

    Hullaballoonatic

    06/05/2019, 7:54 PM
    I believe this is the way that
    List
    and
    MutableList
    and the like are written in stdlib
    r
    s
    • 3
    • 8
  • s

    Saku Ytti

    06/06/2019, 9:35 AM
    Are my options of shipping static binaries graalvm and kotlin-native, or are there other options? I’m not focused on performance, rather focus is easy to run and distribute
    d
    c
    • 3
    • 3
  • k

    Ken McDonald

    06/06/2019, 2:18 PM
    Just getting started in Kotlin and went through the free Udacity course...coming from a C++ background. Question: I understand there are not really the same kind of class constructors in Kotlin like C++ with overloads for different constructors and such, but is the init() function is kind of the same thing? We can have any number of init() inside a Kotlin class; they all run and are executed sequentially as encountered in the class description? There's no way to do conditional init() execution when defining them, only by crafting decision logic inside them?
    s
    i
    • 3
    • 8
  • s

    Seb C

    06/06/2019, 4:42 PM
    Is this graphql-kotlin the easiest way to convert existing Rest API into Graphql
    d
    • 2
    • 4
  • s

    Seb C

    06/07/2019, 12:09 AM
    @Dariusz Kuc thanks for reply but in general what is the best practice to convert a given REST API backend into GraphQL queries
    d
    • 2
    • 4
  • h

    Hullaballoonatic

    06/07/2019, 9:22 PM
    any way i can get around having to bloat all my classes as so for others' convenience:
    fun foo(values: DoubleArray) = //do the things
    fun foo(vararg values: Double) = foo(DoubleArray(values.size) { values[it] })
    fun foo(values: List<Double>) = foo(values.toDoubleArray())
    fun foo(values: Vector) = foo(values.toDoubleArray())
    I can use extension methods and chuck many of them into separate files, but sometimes I can't, like in the case of inner classes or objects. is there a better way that i'm not really seeing? afaik there is absolutely no shared parent class between arrays and lists. my
    Vector
    class at least shares
    Iterable<Double>
    with
    List<Double>
    , but in many cases it's more efficient to duplicate the body of functions the two share instead of unifying them under
    Iterable
    because most methods require
    Iterable::toList
    before I can do
    foo
    things to it.
    k
    • 2
    • 8
  • h

    Hullaballoonatic

    06/07/2019, 9:56 PM
    I have a really hard time recognizing when I should be uses sequences vs when i shouldn't. Fundamentally, I understand that sequences are lazy and execute each element at a time through to the end, and you can conditionally escape the sequence to make things more efficient.... ....but there's actually few common use cases (in my experience) in which they would benefit me. How am I supposed to even keep them in mind?
    👍 1
    r
    t
    • 3
    • 7
  • m

    Mike

    06/08/2019, 12:44 AM
    I’m looking at KotinTest for unit testing, and one of the examples talks about putting tests in “context blocks.”
    class MyTests : FunSpec({
        context("a test group") {
            test("String length should return the length of the string") {
                "sammy".length shouldBe 5
                "".length shouldBe 0
            }
        }
    })
    What would be the purpose of this? Is this a kotlin thing? Or a kotlin test thing. It turns out googling for “kotlintest context” or “kotlin context” doesn’t find much that appears relevant.
    k
    s
    • 3
    • 2
  • g

    Guru

    06/08/2019, 8:02 AM
    In the above code I have purposefully did not close a channel. Can any one tell me how to know if this is the coroutine which is still running/waiting...? (or) in other words.. I wan to debug a coroutine which is still running in IntelliJ. Please help. Normally if it was a thread then we could look at thread dump and figure out which thread is still running. but how do we do that for coroutine?
    v
    • 2
    • 1
  • p

    Peter Kehl

    06/09/2019, 12:44 AM
    Similar with
    class Foo { lateinit var i: Integer }
    var foo= Foo()
    
    foo.i.isInitialized // =>
    // error: unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
    // @SinceKotlin @InlineOnly public val @receiver:AccessibleLateinitPropertyLiteral KProperty0<*>.isInitialized: Boolean defined in kotlin
    
    foo::i.isInitialized // ->error: backing field of 'var i: Integer' is not accessible at this point
    Help, please.
    s
    • 2
    • 5
  • c

    chan

    06/10/2019, 5:44 AM
    abstract class Function<
      TDatabase : Database<*, *>,
      TSchema : Schema<*, *, *>
      >(
      jdbc: JdbcTemplate,
      database: TDatabase,
      schema: TSchema,
      name: String
    ) : SchemaObject<TDatabase, TSchema>(jdbc, database, schema, name) {
    
      open val args: Array<String> = emptyArray()
    
      override fun toString(): String {
        return super.toString() + "(" + Strings.arrayToCommaDelimitedString(args) + ")"
      }
    }
    a
    • 2
    • 1
  • h

    Hullaballoonatic

    06/10/2019, 6:39 PM
    I'm guessing the following is impossible, but I figured it can't hurt to ask:
    class Foo(val title: String = *the name of the field storing the instance of this class*)
    
    val foo = Foo()
    So, in this case "foo"
    s
    k
    • 3
    • 37
  • b

    bjonnh

    06/11/2019, 6:03 PM
    how do I get access to the "main" class in a fun main() ? I need to access something like: MySpec::class.java.getResource("/html/file.html").readText()
    s
    r
    k
    • 4
    • 10
  • s

    scottiedog45

    06/12/2019, 11:06 AM
    This line is not compiling and I have no clue why :
    var phoneOrTablet : String = Resources.getSystem().getBoolean(R.bool.isTablet) ? "Tablet" : "Phone"
    d
    m
    • 3
    • 6
  • s

    spand

    06/12/2019, 11:07 AM
    There is no
    ? :
    in kotlin
    👍 3
    n
    c
    • 3
    • 3
  • p

    poohbar

    06/12/2019, 2:31 PM
    I am bit confused regarding
    as
    and
    as?
    .. In this example
    ref as? String
    the result is
    null
    if the
    ref
    is not a
    String
    or if the
    ref
    is null, correct? I need to cast a reference to String and throw if it is not a String. How can I do that?
    d
    • 2
    • 19
  • s

    scottiedog45

    06/13/2019, 10:40 AM
    -.kt
    m
    w
    a
    • 4
    • 7
  • m

    Marko Mitic

    06/13/2019, 11:22 AM
    any chance this is possible somehow?
    class C: Iface by Delegate(this)
    m
    r
    +2
    • 5
    • 11
  • s

    Slackbot

    06/13/2019, 12:35 PM
    This message was deleted.
    m
    f
    • 3
    • 2
  • b

    bjonnh

    06/13/2019, 5:49 PM
    is there a way to partition a map ? I want to partition according to the presence of the key in a list or not
    t
    p
    • 3
    • 4
  • h

    Hullaballoonatic

    06/13/2019, 9:38 PM
    fun <F: Foo> foo1(fooRef: KProperty1<Bar, Foo>) = ...
    
    fun <F: Foo> foo2(foo: Foo) = foo1(WHAT GOES HERE?)
    Is there a simple way to do this?
    Bar::(something with foo)
    ?
    k
    • 2
    • 7
Powered by Linen
Title
h

Hullaballoonatic

06/13/2019, 9:38 PM
fun <F: Foo> foo1(fooRef: KProperty1<Bar, Foo>) = ...

fun <F: Foo> foo2(foo: Foo) = foo1(WHAT GOES HERE?)
Is there a simple way to do this?
Bar::(something with foo)
?
k

karelpeeters

06/13/2019, 10:23 PM
foo1
takes a property of
Bar
with type
Foo
, and all you've got is an instance or
Foo
. How could that possibly converted to the latter?
h

Hullaballoonatic

06/13/2019, 10:27 PM
so let's say
fooRef: KProperty1<Bar, String>
, e.g.
Bar::name
. How could you engineer
KProperty1<Bar, String>
from
name
?
k

karelpeeters

06/13/2019, 10:31 PM
Do you happen to be confused about how variables work? If I have a class
class Bar(val name: String)
and then I do
foo2(bar.name)
there's no connection to
bar
anymore,
bar.name
is just an ordinary
String
instance out there with everything else
You can't engineer a property from a random instance, it may not even be a property,
foo("test")
, it may be a "property" of multiple classes, it may be in a list somewhere, ...
h

Hullaballoonatic

06/13/2019, 10:32 PM
Yeah, so you can provide the
Bar
part yourself again.
alright. i'll try to think of a different solution
k

karelpeeters

06/13/2019, 10:33 PM
I don't get what you want to happen.
👆 2
➕ 1
View count: 3