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

    Florian

    06/27/2019, 9:29 AM
    Does Array<Int> always translate to Integer[] on the JVM?
    ➕ 2
    k
    r
    • 3
    • 6
  • c

    CamilleBC

    06/27/2019, 10:33 AM
    Hello ! Is there a good way to manage API errors on call in Kotlin? I'm writin a small API library and I'm wondering Should I do: -
    fun getData(): Data?
    -> catch all exceptions and return
    null
    -
    fun getData(): Data
    ->
    throw
    exceptions depending on the situation and have the user of the library catch them -
    fun getData(): Outcome<S: Success, F: Failure>
    -> create a custom class to wrap the result I'm very much a beginner, and I have no clue what would be the best pratice. Btw I'm using
    retrofit2
    with kotlin's coroutines, so the REST requests might return exceptions.
    m
    p
    • 3
    • 5
  • p

    pavel

    06/27/2019, 2:23 PM
    I have a data class that I’ve printed to logs using default
    toString
    . Now I am doing some log manipulation and need to go from that String representation to the data class itself. Is there a simple way to do that?
    • 1
    • 1
  • e

    Edward

    06/29/2019, 9:13 PM
    Hi, i' studying Kotlin and want to make a simple web CRUD application for a book store. Can someone give me some resources to achieve this? I have little experience with web applications, i only did some simple servlets stuff before.
    r
    • 2
    • 5
  • k

    karelpeeters

    06/30/2019, 6:07 PM
    Yes, it's just a representation you can't actually declare intersection types like that yourself.
    f
    i
    • 3
    • 11
  • n

    Nikita

    07/01/2019, 3:50 PM
    Hi! I have strange problem with Kotlin 1.3.40. Can anybody explain this?
    a
    a
    a
    • 4
    • 12
  • p

    poohbar

    07/02/2019, 7:07 PM
    hi!
    list.filter { it.field != null }
        .associateBy({ it.id }, { it.field!! })
    is there a way to avoid
    !!
    on the second line?
    list
    is a list of:
    data class Person(val id: String, val field: Field?)
    d
    p
    k
    • 4
    • 8
  • s

    Slackbot

    07/03/2019, 6:15 PM
    This message was deleted.
    k
    • 2
    • 1
  • v

    Vadim Kotov

    07/04/2019, 8:32 AM
    Can anyone give a link to the docs where this syntax is described? Can't find it here - https://kotlinlang.org/docs/reference/typecasts.html I mean how "other" is smart-casted in place, not assigned to another var.
    w
    k
    • 3
    • 5
  • m

    Mike R

    07/05/2019, 5:30 PM
    Is there a way to get all the properties on an object (for example to print them out)? In Python, I would do
    my_obj.__dir__
    a
    • 2
    • 3
  • f

    Florian

    07/06/2019, 5:56 PM
    in an if check,
    in
    is the same as
    contains
    . What is the operator function for
    in
    when we use it in a loop?
    d
    d
    k
    • 4
    • 36
  • f

    Florian

    07/07/2019, 4:33 PM
    Are Int/Long/CharProgression the only progression in the stdlib?
    k
    • 2
    • 2
  • q

    Quinn

    07/08/2019, 1:33 PM
    Is it possible to generate functions with Kotlin? I would like to take a list of endpoints from an API and their arguments and then build those into methods during runtime. So for example:
    {
      "api.test": [],
      "<http://apps.permissions.info|apps.permissions.info>": [
        "token"
      ],
      "apps.permissions.request": [
        "token",
        "scopes",
        "trigger_id"
      ],
    So in the end I could call my method with myApp.api.test('Bearer s0met0ken') and myApp.apps.permissions.request('Bearer s0met0ken', 'user.write', 'aTrigger') Is this possible with some functional magic?
    c
    • 2
    • 3
  • f

    Florian

    07/08/2019, 8:16 PM
    Or could I also return, say, a String (theoretically)?
    k
    • 2
    • 3
  • f

    Florian

    07/08/2019, 8:51 PM
    why are many functions in the stdlib extension-functions and not put directly into the corresponding class?
    k
    t
    t
    • 4
    • 5
  • i

    Ive Vasiljevic

    07/09/2019, 12:15 PM
    How do inline functions exactly execute multiple lines of code. As an example
    with
    inline function has a receiver and another function that behaves as extension function, so that receiver is able to execute that function on it self. My question is how does that inline function accept multiple lines of code that need to be executed?
    d
    i
    • 3
    • 4
  • h

    Hullaballoonatic

    07/09/2019, 10:34 PM
    What effects does the version of the jvm you target have on kotlin code? I've never written kotlin/jvm that isn't targetting 1.8
    m
    k
    • 3
    • 2
  • u

    ursus

    07/10/2019, 1:31 AM
    best I can do is manually Bar(foo.a, foo.b) right?
    h
    e
    • 3
    • 4
  • u

    ursus

    07/10/2019, 1:32 AM
    and on that note, is it possible to add default params to the abstract vals somehow? if I make them open nonabstract and add defaults there, it wont get applied to subclasses right?
    h
    • 2
    • 2
  • d

    Diogo Ribeiro

    07/10/2019, 8:21 AM
    Hi guys, a quick question...Do I need the (value == null) here? Or when the value is null, the "when" expression will trigger the else statement
    d
    i
    m
    • 4
    • 4
  • t

    tipsy

    07/10/2019, 2:38 PM
    i want to create a usually/sometimes statement for use with mock data. currently i have:
    usually({
    
    }, sometimes {
    
    })
    is it possible to get rid of the parens for
    usually
    somehow, so i just get:
    usually {
    
    } sometimes {
    
    }
    ?
    a
    w
    k
    • 4
    • 15
  • p

    poohbar

    07/11/2019, 2:18 PM
    is there a variant of
    associateBy
    that would throw if a key should have more than one value?
    🇳🇴 1
    t
    • 2
    • 1
  • k

    karelpeeters

    07/12/2019, 10:49 AM
    https://kotlinlang.org/docs/reference/operator-overloading.html
    f
    • 2
    • 1
  • b

    b00m

    07/12/2019, 12:38 PM
    I'm wondering if it's possible to define a type that would like a collection of either a String or another collection of possibly strings or Collections
    k
    • 2
    • 9
  • j

    josh

    07/12/2019, 3:01 PM
    I started Koans (and will still finish them) but the bugs were eating up enough time it's distracting me from learning, lol
    l
    k
    a
    • 4
    • 4
  • f

    Florian

    07/12/2019, 5:34 PM
    why does this not run, but
    test1.equals(test2)
    does?
    k
    a
    • 3
    • 7
  • f

    Florian

    07/13/2019, 10:05 AM
    Also, if I don't override
    equals
    it compares the pointer by default, correct?
    a
    k
    • 3
    • 6
  • b

    b00m

    07/13/2019, 1:27 PM
    i've got a mutable list of <Any>. would like to iterate through it in a for loop.
    k
    • 2
    • 8
  • s

    scottiedog45

    07/14/2019, 1:52 AM
    What exactly is happening with the
    .count
    lambda inside this function; it looks to me like a lambda is being passed to an
    Int
    ? What?
    fun bingo(ticket: Array<Pair<String, Int>>, win: Int) =
        if (ticket.count { it.second.toChar() in it.first } >= win) "Winner!" else "Loser!"
    I’m on https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/count.html and don’t see anything like a closure happening for
    Array.count
    a
    • 2
    • 2
  • c

    CoopCoding

    07/14/2019, 1:55 AM
    Hi, I'm just starting to learn Kotlin and I'm a little confused by something. How come if I assign a function to a varibale, I am able to pass it in to map like this:
    fun main() {
      val strings = listOf("Hello", "World", "!")
      val getLengthOfString = fun(str: String) : Int {
        return str.length
      }
      val lengths = strings.map(getLengthOfString)
      println(lengths)
    }
    But if the function is a regular function, I cant pass in the function name like so:
    fun main() {
      val strings = listOf("Hello", "World", "!")
      fun getLengthOfString(str: String): Int {
        return str.length
      }
      val lengths = strings.map(getLengthOfString)
      println(lengths)
    }
    ❤️ 2
    l
    b
    +2
    • 5
    • 10
Powered by Linen
Title
c

CoopCoding

07/14/2019, 1:55 AM
Hi, I'm just starting to learn Kotlin and I'm a little confused by something. How come if I assign a function to a varibale, I am able to pass it in to map like this:
fun main() {
  val strings = listOf("Hello", "World", "!")
  val getLengthOfString = fun(str: String) : Int {
    return str.length
  }
  val lengths = strings.map(getLengthOfString)
  println(lengths)
}
But if the function is a regular function, I cant pass in the function name like so:
fun main() {
  val strings = listOf("Hello", "World", "!")
  fun getLengthOfString(str: String): Int {
    return str.length
  }
  val lengths = strings.map(getLengthOfString)
  println(lengths)
}
❤️ 2
l

leodeng

07/14/2019, 4:05 AM
you’ll need to use
::getLengthOfString
c

CoopCoding

07/14/2019, 4:15 AM
Ok, thanks.
b

b00m

07/14/2019, 4:42 AM
what does :: do here?
a

Al Warren

07/14/2019, 5:33 AM
Have a look at this https://kotlinlang.org/docs/reference/reflection.html#function-references
c

CoopCoding

07/14/2019, 5:48 AM
@Al Warren Thanks, I'm still a little fuzzy though. Is
::
basically used to satisfy the types? Also, in most other languages,
::
means bind, is that not the case here? Thanks.
s

Shawn

07/14/2019, 2:19 PM
::
isn’t an operator is used to specify function references. It looks a little weird since we’re referencing a function in the local scope. If you wanted to reference a method of a specific class it’d look like
Foo::bar
. Since the function exists within the local scope, there isn’t a name to really qualify its location by, so it comes out to just
::bar
.
::
is used probably because reusing
.
is ambiguous
c

CoopCoding

07/14/2019, 10:03 PM
Ah, got it, thanks. 🙂
👍 1
a

Al Warren

07/14/2019, 10:20 PM
Btw, the docs actually call it an operator. See the link above.
s

Shawn

07/14/2019, 10:24 PM
Apologies, I was imprecise in my wording, I meant more that
::
isn’t like bind or otherwise like
plus
or other infix operators - in this case it would fall in the same category as the dot operator or even
->
in C
View count: 3