{ println(getFirstCharacter("")) // throws StringIndexOutOfBoundsException println(div(1, 0)) // throws ArithmeticException } // impure functions fun getFirstCharacter(s: String): Char { return s.get(0) } fun div(a: Int, b: Int): Int { return a / b } これらを純粋関数にするには、 失敗する可能性のある値 を取り扱うための型を利用する。 例えば Kotlin では、言語標準の Result 型で Result.success と Result.failure の 二種類の値を表現できる。 8