https://kotlinlang.org logo
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
benchmarks
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
confetti
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
lincheck
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
Title
v

Vampire

06/05/2020, 9:07 AM
Is there a better way to achieve this:
enum class Boo(private val fooDelegate: String? = null) {
    BAZ,
    BAM(fooDelegate = "bar");

    val foo: String
        get() = fooDelegate ?: error("no foo set for ${javaClass.simpleName}.${name}")
}
I want that if
foo
is needed, it must have been set, but if it is not used, it is not necessary to set it. Optimally I like to access it by name
foo
and also set it by name
foo
instead of setting it by name
fooDelegate
or
optionalFoo
. When getting
foo
, I want it to be of type
String
though, not
String?
.
t

tddmonkey

06/05/2020, 9:10 AM
My first though is that a sealed class hierarchy might be more appropriate
v

Vampire

06/05/2020, 9:11 AM
But then you can have multiple instances of each class and that is not intended. It is an enum with properties, I just don't want to set the properties if they are not needed, but use them non-null. Or did I misunderstand what you suggest?
t

tddmonkey

06/05/2020, 9:12 AM
They can be implemented as
object
if you only want 1 instance
☝️ 2
v

Vampire

06/05/2020, 9:13 AM
Hm, but still this feels wrong, I also use
Boo.values()
to iterate over all of them and similar things. I would effectively need to recreate the enum functionality I use, wouldn't I?
b

bezrukov

06/05/2020, 9:17 AM
if you have kotlin-reflect dependency, you can write
companion object {
   fun values() = Boo::class.sealedSubclasses.mapNotNull { it.objectInstance }
}
v

Vampire

06/05/2020, 9:19 AM
Well, it still means reprogramming the enum functionality even if it is slim. 😕
Afair I also need the ordering that enums have naturally
So what you two suggest is basically this, right?
sealed class Boo(val ordinal: Int) : Comparable<Boo> {
    val name: String = javaClass.simpleName

    open val foo: String get() = error("no foo set for ${javaClass.superclass.simpleName}.${name}")

    final override fun compareTo(other: Boo) = compareValues(ordinal, other.ordinal)

    object BAZ : Boo(0)

    object BAM : Boo(1) {
        override val foo: String = "bar"
    }

    companion object {
        fun values() = Boo::class.sealedSubclasses.mapNotNull { it.objectInstance }
    }
}
Ah stupid me, when writing the enum I forgot about overwriting properties that I now used in the sealed class. So what I was after is simply
enum class Boo {
    BAZ,
    BAM {
        override val foo = "bar"
    };

    open val foo: String get() = error("no foo set for ${javaClass.simpleName}.${name}")
}
What would be the benefit of using a sealed class hierarchy instead @tddmonkey?
t

tddmonkey

06/05/2020, 12:45 PM
foo
would only exist in the hierarchy that needs it, so no need for runtime errors
v

Vampire

06/05/2020, 12:47 PM
That doesn't make much sense, does it? I'm using
foo
outside. So I have a method with parameter
Boo
and I want to get
foo
on the argument. If someone gives a
Boo
to the method that does not have a value defined for
foo
, this is a programming error.
t

tddmonkey

06/05/2020, 12:49 PM
it’s hard to give more help without seeing how you use it. From the example you give, if the calling code asks for
foo
from a value that doesn’t have it you get a runtime exception. My preference would be to switch that around so the type system doesn’t allow it
v

Vampire

06/05/2020, 1:01 PM
Hm, with
Boo
,
BooWithFoo : Boo
and then giving
BooWithFoo
to the class, I see, makes sense of course ... maybe. Thanks for that, I'll think about it.
But for the ordering I then still need to define the ordinals manually
t

tddmonkey

06/05/2020, 1:10 PM
yeah - but for something like that I always prefer it being explicit rather than being implicit in code ordering
v

Vampire

06/05/2020, 1:17 PM
Uh, now it gets ugly.
sealed class Boo(val ordinal: Int) : Comparable<Boo> {
    val name: String = javaClass.simpleName
    final override fun compareTo(other: Boo) = compareValues(ordinal, other.ordinal)

    sealed class BooWithFoo(
            ordinal: Int,
            val foo: String
    ) : Boo(ordinal)

    object BAZ : Boo(0)
    object BAM : BooWithFoo(1, "bar")

    companion object {
        fun values() = Boo::class.sealedSubclasses.mapNotNull { it.objectInstance }
    }
}
does not work, BAM needs to be defined inside
BooWithFoo
and
values()
only finds
BAZ
as only direct subclasses are returned 😕
So now it is
sealed class Boo(val ordinal: Int) : Comparable<Boo> {
    val name: String = javaClass.simpleName
    final override fun compareTo(other: Boo) = compareValues(ordinal, other.ordinal)

    sealed class BooWithFoo(
            ordinal: Int,
            val foo: String
    ) : Boo(ordinal) {
        object BAM : BooWithFoo(1, "bar")
    }

    object BAZ : Boo(0)

    companion object {
        fun values() = listOf(Boo::class, BooWithFoo::class)
                .flatMap { it.sealedSubclasses }
                .mapNotNull { it.objectInstance }
    }
}
😕
t

tddmonkey

06/05/2020, 2:06 PM
I would just put the
values()
in another object so its explicit that way, so something like:
object Boos{
   val allBoosInOrder() = listOf(
       Foo
       Bar
   )
}
v

Vampire

06/05/2020, 2:09 PM
Of course I can throw more boilerplate at it, but Kotlin is meant to reduce boilerplate isn't it? 😄 Besides that, the
values()
is not my main concern, more the ripped apart constants.
Hm, maybe like this:
sealed class Boo(val ordinal: Int) : Comparable<Boo> {
    val name: String = javaClass.simpleName
    final override fun compareTo(other: Boo) = compareValues(ordinal, other.ordinal)

    interface WithFoo {
        val foo: String
    }

    object BAZ : Boo(0)
    object BAM : Boo(1), WithFoo {
        override val foo = "bar"
    }

    companion object {
        fun values() = Boo::class.sealedSubclasses.mapNotNull { it.objectInstance }.sorted()
    }
}
But then I still need to receive
Boo
in the method and then check for
is WithFoo
as other classes could implement the
WithFoo
interface and I also need the other properties in the method.
Ah, where clause on generic function parameter and we are fine maybe
sealed class Boo(val ordinal: Int) : Comparable<Boo> {
    val name: String = javaClass.simpleName
    final override fun compareTo(other: Boo) = compareValues(ordinal, other.ordinal)

    interface WithFoo {
        val foo: String
    }

    object BAZ : Boo(0)
    object BAM : Boo(1), WithFoo {
        override val foo = "bar"
    }

    companion object {
        fun values() = Boo::class.sealedSubclasses.mapNotNull { it.objectInstance }.sorted()
    }
}

fun <T> hoo(boo: T) where T : Boo, T : Boo.WithFoo {
    println(boo.name)
    println(boo.foo)
}
Ok, so I think this will be the final solution, thanks:
sealed class Boo(val ordinal: Int) : Comparable<Boo> {
    val name: String = javaClass.simpleName

    init {
        if (!usedOrdinals.add(ordinal)) {
            throw IllegalArgumentException("${javaClass.superclass.simpleName}.$name: " +
                    "The ordinal $ordinal is already used by another instance")
        }
    }

    final override fun compareTo(other: Boo) = compareValues(ordinal, other.ordinal)

    object BAZ : Boo(0)
    object BAM : Boo(1), WithFoo {
        override val foo = "bar"
    }

    companion object {
        private val usedOrdinals = mutableSetOf<Int>()

        fun values() = Boo::class
                .sealedSubclasses
                .mapNotNull { it.objectInstance }
                .sorted()
    }

    interface WithFoo {
        val foo: String
    }
}

// verify ordinals are unique
private val booValues = Boo.values()

fun <T> hoo(boo: T) where T : Boo, T : Boo.WithFoo {
    println(boo.name)
    println(boo.foo)
}
Hm, no, giving the ordinals manually is tedious, especially when new constants get added in the middle or beginning. 😕
t

tddmonkey

06/06/2020, 9:57 AM
Do you need the actual ordinal, or is it just used for ordering?
v

Vampire

06/08/2020, 8:25 AM
I think only for ordering
t

tddmonkey

06/08/2020, 8:41 AM
so for that I would skip having each object know about its ordinal and the complication that comes from that and instead just store them in a list somewhere that maintains the ordering. This removes any problems with duplicate ordering and lists are designed to order things by ordinal
v

Vampire

06/08/2020, 8:45 AM
But it forces to have each object twice, once for declaring it, once for ordering it. Besides having two long lists this adds the risk of forgetting to adjust one place when editing the other. That's the nice thing with enums that are automatically ordered by declaration order. 😕
t

tddmonkey

06/08/2020, 8:57 AM
Perhaps I missed something - I don’t know why you’d have to have it twice
v

Vampire

06/08/2020, 9:02 AM
This is what I understood you suggested:
sealed class Boo() : Comparable<Boo> {
    val name: String = javaClass.simpleName

    final override fun compareTo(other: Boo): Int {
        val indexOfThis = order.indexOf(this)
        if (indexOfThis == -1) {
            throw AssertionError("$name is missing in 'order' list")
        }
        val indexOfOther = order.indexOf(other)
        if (indexOfOther == -1) {
            throw AssertionError("${other.name} is missing in 'order' list")
        }
        return compareValues(indexOfThis, indexOfOther)
    }

    object BAZ : Boo()
    object BAM : Boo(), WithFoo {
        override val foo = "bar"
    }

    companion object {
        private val order = listOf(
                BAZ,
                BAM
        )
        
        fun values() = Boo::class
                .sealedSubclasses
                .mapNotNull { it.objectInstance }
                .sorted()
    }

    interface WithFoo {
        val foo: String
    }
}
So I need each twice and with a list of 60 values it is two long lists
b

bezrukov

06/08/2020, 9:07 AM
I believe Boo::class.sealedSubclasses returns classes in the order from source code
v

Vampire

06/08/2020, 9:08 AM
But is that guaranteed or a coincidence? I didn't see that defined in the docs or
sealedSubclasses
t

tddmonkey

06/08/2020, 9:09 AM
I think you can collapse it down to just this:
sealed class Boo() : Comparable<Boo> {
    val name: String = javaClass.simpleName
    object BAZ : Boo()
    object BAM : Boo(), WithFoo {
        override val foo = "bar"
    }

    companion object {
        private val order = listOf(
                BAZ,
                BAM
        )
        fun values() = order
    }
    interface WithFoo {
        val foo: String
    }
}
And then of course you could just remove
values()
and make
order
public
v

Vampire

06/08/2020, 9:11 AM
Yeah, of course I don't need the
sealedSubclasses
reflection anymore if I define the order explicitly. I don't see how I could remove the
compareTo
method override though.
And this wouldn't change that I have two lists with 60+ entries
t

tddmonkey

06/08/2020, 9:14 AM
ah, it doesn’t need to implement
Comparable
anymore - you did that to do the sorting in the old
values()
method.
Again - im not sure why you have 2 lists?
v

Vampire

06/08/2020, 9:14 AM
So it would be this:
sealed class Boo() : Comparable<Boo> {
    val name: String = javaClass.simpleName

    final override fun compareTo(other: Boo): Int {
        val indexOfThis = values.indexOf(this)
        if (indexOfThis == -1) {
            throw AssertionError("$name is missing in 'order' list")
        }
        val indexOfOther = values.indexOf(other)
        if (indexOfOther == -1) {
            throw AssertionError("${other.name} is missing in 'order' list")
        }
        return compareValues(indexOfThis, indexOfOther)
    }

    object BAZ : Boo()
    object BAM : Boo(), WithFoo {
        override val foo = "bar"
    }

    companion object {
        val values by lazy {
            listOf(
                    BAZ,
                    BAM
            )
        }
    }

    interface WithFoo {
        val foo: String
    }
}
Which still has the problem of both lists
> ah, it doesn’t need to implement 
Comparable
 anymore Yes it does, or is not comparable > you did that to do the sorting in the old 
values()
 method. You are assuming too much, I made it comparable because I need it comparable, I just used it in
values
as the list should be sorted > Again - im not sure why you have 2 lists?
@bezrukov hm, do you have another idea on how to get the list of object instances?
kotlin-reflect
is there and I can get the
sealedSubclasses
. But unfortunatley there is a
SecurityManager
in place that denies
("java.lang.RuntimePermission" "accessDeclaredMembers")
, so the
it.objectInstance
fails with
AccessControlException
. :-(
Hm, I think I just switch back to
enum
. What I really would like to have is (https://youtrack.jetbrains.com/issue/KT-39466)
enum class Boo {
    BAZ,
    BAM : WithFoo {
        override val foo = "bar"
    };
}

interface WithFoo {
    val foo: String
}
But I guess I'll go with
enum class Boo {
    BAZ,
    BAM {
        override val foo = "bar"
    };

    open val foo: String
        get() = error("no foo set for ${javaClass.simpleName}.${name}")
}
for now, that at least removes the
fooDelegate
naming need
t

tddmonkey

06/08/2020, 1:57 PM
fair enough!
v

Vampire

06/08/2020, 2:00 PM
Or maybe
enum class Boo {
    BAZ,
    BAM {
        val foo = "bar"
    };
}

fun foo(t: Boo) = "${t.name}: ${t.javaClass.kotlin.memberProperties.find { it.name == "foo" }?.get(t) ?: error("no foo")}"
if that works. But I think the other variant where I only have to do the erroring centrally and do not need reflection
Thanks for your help and patience though 🙂
t

tddmonkey

06/08/2020, 2:01 PM
No problem, just sad we couldn’t come up with anything better
v

Vampire

06/08/2020, 2:03 PM
At least I learned some new things and in the end at least have it a bit nicer and a hope for improvement if my issue gets considered in the language design 🙂
t

tddmonkey

06/08/2020, 2:14 PM
If you learned something, we can both walk away happy 🙂
👌 1