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

    jurajsolarml

    12/17/2018, 1:29 PM
    Hello,please solve the problems.Registration does not work where can be an error ? Thanks https://hastebin.com/punafomaze.cpp
    r
    t
    • 3
    • 3
  • u

    ursus

    12/17/2018, 4:12 PM
    How can I have call site generic in interface property?
    interface FooProvider {
        fun <T> getFoo() : T <--- works
        val <T> foo: T <--- doesnt
    }
    d
    s
    +5
    • 8
    • 294
  • r

    Ryan Benasutti

    12/18/2018, 3:07 AM
    Is this supposed to be valid Kotlin? The compiler can't parse having braces back-to-back like that.
    -.kt
    k
    • 2
    • 3
  • s

    Shawn

    12/18/2018, 4:18 PM
    I’m not sure what
    masterplanRepository.save()
    returns (maybe an instance of the saved record), but if it’s just
    Unit
    , I might suggest writing
    .apply(masterplanRepository::save)
    instead
    t
    • 2
    • 1
  • h

    hho

    12/19/2018, 12:51 PM
    S
    is the type of the accumulator (and therefore also the return type).
    a
    b
    • 3
    • 5
  • u

    ursus

    12/20/2018, 4:52 AM
    Is there a easy way to "pretty print" toString of a dataclass (indentations, new lines etc) ?
    d
    s
    r
    • 4
    • 7
  • u

    ursus

    12/21/2018, 3:41 AM
    -- if I rename the class to WhateverViewModel it all works lol
    o
    k
    a
    • 4
    • 11
  • d

    damian

    12/21/2018, 11:23 AM
    If this will filter my sensorType intArray to return a reduced array where values == 6
    val indexes = sensorType.filter { sensorType -> sensorType == 6 }
    ... how do I instead get an array of the matched indexed positions?
    u
    • 2
    • 2
  • p

    poohbar

    12/21/2018, 3:46 PM
    how would you implement this java interface in kotlin if you want the output type to be equivalent of
    void
    ?
    public interface RequestHandler<I, O> {
        public O handleRequest(I input, Context context);
    }
    n
    a
    • 3
    • 3
  • p

    poohbar

    12/21/2018, 3:47 PM
    I tried making
    O
    a
    Nothing
    but that wouldn't compile.. I also tried
    java.lang.Void
    to no avail.
    s
    d
    • 3
    • 2
  • s

    Smorg

    12/24/2018, 4:49 AM
    Hi everyone, glad to be here. I am trying to create an extension property, but I want to avoid creating the object that the getter returns every time I am accessing the property. Does anyone have an idea how I can implement that please?
    s
    s
    • 3
    • 15
  • m

    Miguel

    12/24/2018, 4:14 PM
    Hello! Can someone tell me why can’t I do this?
    s
    p
    • 3
    • 23
  • s

    Smorg

    12/24/2018, 10:15 PM
    quick one: Please, who knows an idiomatic way to select a previous or next element in a collection in Kotlin?
    k
    s
    p
    • 4
    • 5
  • k

    Kevin

    12/25/2018, 5:43 AM
    Hey I have an anonymous object structure that I want to take as a parameter in a function, what do I put for the type on the parameter?
    s
    • 2
    • 4
  • k

    Kevin

    12/25/2018, 10:12 AM
    I want to have a constructor that takes different parameters, does some computations, and then calls the primary constructor with the result of that computation.
    o
    • 2
    • 2
  • k

    Kevin

    12/25/2018, 10:12 AM
    But it looks like that is not possible?
    p
    e
    • 3
    • 10
  • k

    Kevin

    12/25/2018, 11:12 AM
    I want to do this:
    when(x)
        < 1 -> ...
        in 2..5 -> ...
        6 -> ...
        else -> ... (greater than 6)
    i
    • 2
    • 1
  • k

    Kevin

    12/26/2018, 1:51 AM
    I have an abstract parent class A and child classes B and C. I want to declare an abstract method in A such that the implementation in B and C must have a return type of B or C respectively. Is there a way to do this?
    s
    u
    +2
    • 5
    • 6
  • j

    johnfn

    12/26/2018, 6:30 PM
    got it, i just used a setter/getter
    s
    • 2
    • 5
  • s

    Sam

    12/26/2018, 10:41 PM
    Is there any way to get reified type in a class function?
    class Container<T> {
        fun useContainedClassType() {
            T::class.java
        }
    }
    d
    k
    • 3
    • 7
  • a

    Ameho Chan

    12/27/2018, 2:46 AM
    I have a question on writing kotlin test with JUnit5. Why would a extra module name be added to the test method? This example was running on latest IntelliJ IDEA.
    m
    • 2
    • 1
  • s

    Shahzad Aslam

    12/27/2018, 3:21 PM
    Is it possible to access copy function of data class in Java with optional parameters? e.g I want to change a3 in Java. What is the best way to do it considering my class A is immutable. Should I write setter method which internally should do a copy by updating the required value or is there any other option?
    Is_it_possible_to_access_copy_function_of_data_class_in_Java_with_optional_parameters_.kt
    d
    • 2
    • 4
  • j

    JoshK

    12/28/2018, 10:57 PM
    Hey all. I'm new. I was fiddling around with generics and type inference using functional approaches, but I'm a little confused about type inference logic for second order functions. Given
    fun <E> firstOrderFnWithSecondOrderTypeInference() = { value: E -> value }
    , attempting to
    firstOrderFnWithSecondOrderTypeInference()(42)
    gives me a type inference error:
    Type inference failed: Not enough information to infer parameter E in  fun <E> firstOrderFnWithSecondOrderTypeInference( ) : (E) → E Please specify it explicitly.
    firstOrderFnWithSecondOrderTypeInference<Int>()(1234)
    works because there's no type inference for
    E
    of the second rank function. I guess what I'm looking for is a why it's like this so I can understand the language a bit better. I know that C++ is the same way, same with Typescript, so I wonder if there's an underlying reason.
    p
    • 2
    • 2
  • s

    Shawn

    12/29/2018, 7:28 AM
    namespacing is (mostly) handled using
    package
    directives and class names rather than filenames
    d
    • 2
    • 6
  • d

    dave08

    12/30/2018, 3:12 PM
    Is there any ideomatic way to encode a Long into base64 and vise-versa in Kotlin?
    s
    • 2
    • 1
  • j

    johnfn

    12/30/2018, 7:45 PM
    how come i can't do list.forEach(println) and i have to do list.forEach(::println)?
    m
    • 2
    • 1
  • s

    Slackbot

    01/02/2019, 10:37 AM
    This message was deleted.
    t
    f
    • 3
    • 2
  • p

    poohbar

    01/02/2019, 1:47 PM
    Coming from Java where nullability is sort of a free for all, I wonder how do you resolve this problem. I have a
    User
    data class which has a
    type
    field. The type can be either
    REGULAR
    or
    ADMIN
    . When it is of type
    ADMIN
    the user will always have a not null
    companyId
    but when it is a
    REGULAR
    user the
    companyId
    will always be null. Therefore the
    companyId
    now has to be nullable and I am forced to handle it even in this case:
    val user = db.getUser(id)
    if (user.type == ADMIN) {
      // user.companyId is definitely NOT NULL here but the compiler obviously has no idea
    } else {
      // user is a REGULAR user so the companyId is null
    The only way around this is to have two different classes: an
    AdminUser
    and
    RegularUser
    that will reflect the reality more closely. Is there a better way? In Java the compiler does not care and I get no warnings.
    h
    r
    +4
    • 7
    • 18
  • l

    lsk

    01/02/2019, 10:03 PM
    What is the most clean way to rewrite this in kotlin
    if ( a == null ) {
    do something 
    } else {
    do something else
    }
    ☝️ 1
    d
    s
    +4
    • 7
    • 10
  • h

    hates

    01/03/2019, 10:12 AM
    What is best practice in terms of multiple classes in a single file? For example if I have an interface and a (currently) single implementation, I like the idea of keeping them together in a single file, but then sometimes feel I should do how I would in Java and split it into two.
    e
    • 2
    • 2
Powered by Linen
Title
h

hates

01/03/2019, 10:12 AM
What is best practice in terms of multiple classes in a single file? For example if I have an interface and a (currently) single implementation, I like the idea of keeping them together in a single file, but then sometimes feel I should do how I would in Java and split it into two.
e

Egor Trutenko

01/03/2019, 10:20 AM
Official coding conventions has a great paragraph about that:
Placing multiple declarations (classes, top-level functions or properties) in the same Kotlin source file is encouraged as long as these declarations are closely related to each other semantically and the file size remains reasonable (not exceeding a few hundred lines).
You can read the whole conventions here: https://kotlinlang.org/docs/reference/coding-conventions.html
🤩 1
h

hates

01/03/2019, 10:23 AM
Brilliant. Thanks a lot. I should probably read that a couple of times! 🙂
View count: 2