<@U5EFLTPJ5> commented on <@U4U3EK3FB>’s file <htt...
# getting-started
u
@efemoney commented on @hvalls’s file https://kotlinlang.slack.com/files/U4U3EK3FB/F7C5RHFCH/how_can_refactor_this_to_not_to_duplicate_code_.kt:
Copy code
class Position(val x: Int, val y: Int) {

    fun right(units: Int) = Position(x + units.assertAsPositive, y)

    fun left(units: Int) = Position(x - units.assertAsPositive, y)

    fun up(units: Int) = Position(x, y + units.assertAsPositive)

    fun down(units: Int) = Position(x, y - units.assertAsPositive)
    
    val Int.assertAsPositive: Int
        get() = if (this < 0) throw IllegalArgumentException() else this
}