Kotlin Koans 09 Extension functions

Extension functions (Playground)

Description

Read about extension functions. Then implement extension functions Int.r() and Pair.r() and make them convert Int and Pair to RationalNumber.

拡張関数 を読んでください。拡張関数 Int.r()Pair.r() を実装し、IntとPairを RationalNumber に変換してください。

Code

fun Int.r(): RationalNumber = RationalNumber(this, 1)
fun Pair<Int, Int>.r(): RationalNumber = RationalNumber(first, second)

data class RationalNumber(val numerator: Int, val denominator: Int)

Memo

← Blog