Last active
January 26, 2018 09:45
-
-
Save tikidunpon/7dad5e62e0c1518cd1feb5a0d0426e2c to your computer and use it in GitHub Desktop.
12章Null安全
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main(args: Array<String>){ | |
// Null安全 | |
/** | |
* p171 | |
* Javaにおけるnull | |
*/ | |
// Javaでは参照をデリファレンスした時にnullだとNPE(NullPointerException)が起きる | |
/** | |
* p174 | |
* Null安全という考え方 | |
*/ | |
// REPLで実行 | |
/** | |
* p176 | |
* スマートキャスト | |
*/ | |
val a: String? = null | |
val b: String? = "Hello" | |
if (a != null) { | |
println(a.toUpperCase()) | |
} | |
if (b != null) { | |
println(b.toUpperCase()) | |
} | |
val list: List<Any> = listOf(1, 'a', false) | |
for (e in list) { | |
val result: Any? = when(e) { | |
is Int -> e + 5 | |
is Char -> e.toUpperCase() | |
is Boolean -> e.not() | |
else -> null | |
} | |
println(result) | |
} | |
/** | |
* p178 | |
*/ | |
// nullならnullを返すだけ | |
run { | |
val a: Int? = 5 | |
val aInc: Int? = | |
if(a != null) a.inc() | |
else null | |
println("aInc = $aInc") | |
} | |
// 安全呼び出し | |
run { | |
val a: Int? = 5 | |
val aInc: Int? = a?.inc() | |
println("aInc = $aInc") | |
} | |
// NotNullな引数を取る関数にNullableを渡す | |
run { | |
fun square(i: Int): Int = i * i | |
val a: Int? = 5 | |
val aSquare = | |
if (a != null) square(a) | |
else null | |
println("aSquare = $aSquare") | |
} | |
// 拡張関数letを使ったシンプルに書くテクニック | |
// public inline fun <T, R> T.let(block: (T) -> R): R { | |
run { | |
fun square(i: Int): Int = i * i | |
val a: Int? = 5 | |
val aSquare = a?.let { square(it) } | |
// こっちでも可 | |
// val aSquare = a?.let(::square) | |
println("aSquare = $aSquare") | |
} | |
/** | |
* p180 | |
* !!演算子 | |
*/ | |
// !!でNotNullに変換される | |
run { | |
val foo: String? = "Hello" | |
val bar: String = foo!! | |
bar.toUpperCase() | |
println("bar = $bar") | |
} | |
// nullに!!を適用すると、例外をスローする | |
run { | |
// val hoge: String? = null | |
// val fuga: String = hoge!! | |
} | |
// nullに!!を適用すると、例外をスローする | |
run { | |
val foo: String? = "Hello" | |
val bar: String = requireNotNull(foo, {"nullなわけがない"}) | |
bar.toUpperCase() | |
val hoge: String? = null | |
//val fuga: String = requireNotNull(hoge, {"hogeはnullであってはダメ"}) | |
} | |
/** | |
* p181 | |
* エルビス演算子 | |
*/ | |
// nullでなければそれを、nullならデフォルト値のパターン | |
run { | |
val foo: String? = "Hello" | |
(if(foo != null) foo else "default").toUpperCase() | |
val hoge: String? = null | |
(if(hoge != null) hoge else "default") | |
} | |
// エルビス演算子で楽にかける | |
run { | |
val foo: String? = "Hello" | |
val ret = foo ?: "default".toUpperCase() | |
println("ret = $ret") | |
val hoge: String? = null | |
val ret2 = hoge ?: "default" | |
println("ret = $ret2") | |
} | |
// NotNullへの変換に失敗したら任意のエラーを投げられる | |
run { | |
val foo: String? = "Hello" | |
foo ?: throw AssertionError() | |
val hoge: String? = null | |
//hoge ?: throw AssertionError() | |
} | |
/** | |
* p183 | |
* 安全キャスト | |
*/ | |
run { | |
val str: Any = "本当は文字列" | |
str as String | |
//str as Int | |
str as? Int | |
} | |
// 補足: コレクションの扱い | |
// https://kotlinlang.org/docs/reference/null-safety.html#collections-of-nullable-type | |
run { | |
val nullableList: List<Int?> = listOf(1, 2, null, 4) | |
val list: List<Int> = nullableList.filterNotNull() | |
println("list = $list") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment