I was today year old when I learned you can `overr...
# advent-of-code
j
I was today year old when I learned you can
override fun toString()
on value classes and it works!
Copy code
@JvmInline
value class LongPos(val value: Long) {
    override fun toString() = "$row,$col"
}

fun longPos(row: Int, col: Int) = LongPos((row.toLong() shl 32) or (col.toLong() and 0xFFFF_FFFFL))
val LongPos.row get() = value.shr(32).toInt()
val LongPos.col get() = (value and 0xFFFF_FFFFL).toInt()
This will make my debugging somewhat easier 🙂
n
Question: why not also add
Copy code
constructor(row: Int, col: Int)): this((row.toLong() shl 32) or (col.toLong() and 0xFFFF_FFFFL))
j
of course it's also possible, I think I'll also have other implementations. like:
Copy code
typealias PairPos = Pair<Int, Int>
fun PairPos.row get() = first
fun PairPos.row get() = second
And in my code I'll be just using
Copy code
typealias Pos = PairPos // or = LongPos
but maybe it's overcomplication, I'll see 🙂
n
Yeah, I normally use a
data class Point(val x: Int, val y: Int)
in my AoC code. Pretty sure your LongPos is more efficient, but I normally do not care so much about efficiency of my AoC code (as long as it finishes within some seconds), and instead care more about code prettiness (of course also very subjective).