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
Chris Lee
05/30/2022, 1:12 AM
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
Mark
05/30/2022, 1:17 AM
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
An enum, which then allows extending intro the integers with
List<Digit>
👌 2
🧌 1
👍 1
j
Joffrey
05/30/2022, 7:45 AM
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
ephemient
05/30/2022, 8:13 AM
(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)
)
ephemient
05/30/2022, 8:15 AM
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