Which type would you use to represent a single dig...
# getting-started
m
Which type would you use to represent a single digit
[0-9]
? 1️⃣ Int 2️⃣ UInt 3️⃣ Byte 4️⃣ Short 5️⃣ value class of Int 6️⃣ value class of UInt 7️⃣ value class of Byte 8️⃣ value class of Short 9️⃣ value class of Char I suppose we already have the Kotlin answer, which is
Char.digitToInt(): Int
c
Depends on the context. If it is the character representation,
Char
; if it is truly a number (escape code, binary protocol, etc) then
Byte
may be a better choice. All depends on what it represents and how it will be used.
m
Hmm, difficult to answer this question. I would say it’s a number that is often represented as a character. EDIT - actually thinking about this more, in my context the digit is more of a code to represent something else. There is no sense of +1 etc. Although my use case is not this, think of something like a numeric options menu
e
👍 1
r
An enum, which then allows extending intro the integers with
List<Digit>
👌 2
🧌 1
👍 1
j
If you don't want arithmetic and your numbers are effectively just an ordered set of independent values, I would also probably go with the enum. But yeah it really depends on the use case. A value class backed by a
Byte
is also a good option
e
(until we get support for value arrays, there's no real difference at runtime between a
value class Digit(digit: Byte)
and a
value class Digit(digit: Int)
)
it could be interesting if Kotlin were to gain
value enum class
in the future, to combine the benefits of
enum class
(bounded values) with a
value class
(primitive representation), but for now there's just different trade-offs
m
A
value enum class
would make sense for my use case, but as it is
enum class
feels like overkill.