memo

Table of Contents

ループ

5 downTo 1 step 2 の IntProgression は Iterable なので、forEach の他にも map などのファンクションが使える
Int.downTo は IntProgression を返す
step は IntProgression のプロパティ

while

whileループの中から一定の条件になった時、戻り値を返す
(目的・条件)

(例) WEBサーバから受信したヘッダの Last-Modified ヘッダの日付を返す 10回リトライして接続できない場合は例外をスローする => つまり null は返さない

fun getLastModified( url: String ): Date {
    var retryCount = 0

    while( true ) {
        try {
            val conn = URL( url ).openConnection()
            println( conn.inputStream.reader().use() { it.readText() }
            return Date( conn.lastModified )
        } catch( ex: Exception ) {
            if( retryCount == 10 ) {
                throw ex
            }
            println( ex.message )
        }
    }
}

break, continue

forEach

when

try/catch

その他