https://kotlinlang.org logo
#codereview
Title
# codereview
s

Sam Stone

10/05/2023, 11:38 PM
I need an enum of magic constants, but the constants themselves are meaningful. Without getting into the details, there are certain religious astronomical calculations that are referred to by the degree-offset they use, making numbers like "19.8" meaningful in and of themselves (e.g. one might ask, "Which opinion do you hold of? 19.8?" - optionally adding "degrees" afterwards). I am writing a library for these calculations, and it would be a lot of typing to write
DegreeOffset.NINETEEN_POINT_EIGHT_DEGREES
so often. However, it is illegal to write
DegreeOffset.19.8
, and I don't want to resort to `DegreeOffset.`19.8``. What is the best language construct/naming convention to use, and should this become a feature request for one?
DegreeOffset._19_8
maybe? Looks wrong.
h

hho

10/06/2023, 10:31 AM
I'd probably go with
DegreeOffset.NINETEEN_EIGHT
. With autocomplete, it's not that much typing (
<http://DOff.NE|DOff.NE><Tab>
).
s

sbyrne

10/06/2023, 4:42 PM
I would probably use
DegreeOffset._19_8
, or
DegreeOffset.DO_19_8
if you really do not like leading underscores.
k

Klitos Kyriacou

10/06/2023, 4:57 PM
Not sure if it's a good idea myself, but how about...
Copy code
@JvmInline
value class DegreeOffset(val offset: float) {
    init {
        require(offset in arrayOf(..., 19.8, ...)
    }
}

Usage: foobar(DegreeOffset(19.8))
I know floating-point numbers shouldn't be compared for exactness, but as long as all instances are literals, it should be ok.
v

vanshg

10/06/2023, 5:28 PM
resort to `DegreeOffset.`19.8``
IMO, this is exactly the sort of situation that the
syntax is helpful for
1