├── .circleci └── config.yml ├── README.md ├── docs ├── all-open-plugin.md ├── array.md ├── collection-drop.md ├── collection-filter.md ├── collection-for-each.md ├── collection-map.md ├── collection-merge.md ├── collection-sum.md ├── collection-take.md ├── collection-zip.md ├── coroutines-for-kotlin.md ├── enum.md ├── extension.md ├── framework-for-kotlin.md ├── function.md ├── function_object.md ├── function_type.md ├── gradle-kts.md ├── hello-kotlin-android.md ├── if-function.md ├── if-statement.md ├── index.md ├── instance.md ├── kclass-argument.md ├── keyword-for-kotlin.md ├── kotlin-collection-framework.md ├── kotlin-install.md ├── kotlin-plugin-androidstudio.md ├── kotlin-plugin-eclipse.md ├── kotlin-plugin-gradle.md ├── kotlin-plugin-intellij.md ├── kotlin-plugin-netbeans.md ├── loop.md ├── orm-for-kotlin.md ├── reflection.md ├── todo.md ├── try-with-resources-for-kotlin.md ├── type-alias.md ├── type-safe-builder.md ├── type.md ├── unit-test-for-kotlin.md ├── variable.md └── when.md ├── images └── Kotlin-icon.png ├── index.php ├── mkdocs.yml ├── site ├── 404.html ├── all-open-plugin │ └── index.html ├── array │ └── index.html ├── base.html ├── collection-drop │ └── index.html ├── collection-filter │ └── index.html ├── collection-for-each │ └── index.html ├── collection-map │ └── index.html ├── collection-merge │ └── index.html ├── collection-sum │ └── index.html ├── collection-take │ └── index.html ├── collection-zip │ └── index.html ├── content.html ├── coroutines-for-kotlin │ └── index.html ├── css │ ├── base.css │ ├── bootstrap-custom.min.css │ ├── font-awesome-4.5.0.css │ └── highlight.css ├── enum │ └── index.html ├── extension │ └── index.html ├── fonts │ ├── fontawesome-webfont.eot │ ├── fontawesome-webfont.svg │ ├── fontawesome-webfont.ttf │ └── fontawesome-webfont.woff ├── framework-for-kotlin │ └── index.html ├── function │ └── index.html ├── function_object │ └── index.html ├── function_type │ └── index.html ├── gradle-kts │ └── index.html ├── hello-kotlin-android │ └── index.html ├── if-function │ └── index.html ├── if-statement │ └── index.html ├── img │ ├── favicon.ico │ └── grid.png ├── index.html ├── instance │ └── index.html ├── js │ ├── base.js │ ├── bootstrap-3.0.3.min.js │ ├── highlight.pack.js │ └── jquery-1.10.2.min.js ├── kclass-argument │ └── index.html ├── keyword-for-kotlin │ └── index.html ├── kotlin-collection-framework │ └── index.html ├── kotlin-install │ └── index.html ├── kotlin-plugin-androidstudio │ └── index.html ├── kotlin-plugin-eclipse │ └── index.html ├── kotlin-plugin-intellij │ └── index.html ├── license │ └── highlight.js │ │ └── LICENSE ├── loop │ └── index.html ├── mkdocs │ ├── js │ │ ├── lunr.min.js │ │ ├── mustache.min.js │ │ ├── require.js │ │ ├── search-results-template.mustache │ │ ├── search.js │ │ └── text.js │ └── search_index.json ├── nav-sub.html ├── nav.html ├── orm-for-kotlin │ └── index.html ├── reflection │ └── index.html ├── sitemap.xml ├── toc.html ├── todo │ └── index.html ├── try-with-resources-for-kotlin │ └── index.html ├── type-alias │ └── index.html ├── type-safe-builder │ └── index.html ├── type │ └── index.html ├── unit-test-for-kotlin │ └── index.html ├── variable │ └── index.html └── when │ └── index.html └── themes ├── 404.html ├── base.html ├── content.html ├── css ├── base.css ├── bootstrap-custom.min.css ├── font-awesome-4.5.0.css └── highlight.css ├── fonts ├── fontawesome-webfont.eot ├── fontawesome-webfont.svg ├── fontawesome-webfont.ttf └── fontawesome-webfont.woff ├── img ├── favicon.ico └── grid.png ├── js ├── base.js ├── bootstrap-3.0.3.min.js ├── highlight.pack.js └── jquery-1.10.2.min.js ├── license └── highlight.js │ └── LICENSE ├── nav-sub.html ├── nav.html └── toc.html /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | # This configuration was automatically generated from a CircleCI 1.0 config. 2 | # It should include any build commands you had along with commands that CircleCI 3 | # inferred from your project suructure. We strongly recommend you read all the 4 | # comments in this file to understand the structure of CircleCI 2.0, as the idiom 5 | # for configuration has changed substantially in 2.0 to allow arbitrary jobs rather 6 | # than the prescribed lifecycle of 1.0. In general, we recommend using this generated 7 | # configuration as a reference rather than using it in production, though in most 8 | # cases it should duplicate the execution of your original 1.0 config. 9 | version: 2 10 | jobs: 11 | build: 12 | working_directory: ~/yyYank/kotlin-rev-solution 13 | parallelism: 1 14 | shell: /bin/bash --login 15 | # CircleCI 2.0 does not support environment variables that refer to each other the same way as 1.0 did. 16 | # If any of these refer to each other, rewrite them so that they don't or see https://circleci.com/docs/2.0/env-vars/#interpolating-environment-variables-to-set-other-environment-variables . 17 | environment: 18 | CIRCLE_ARTIFACTS: /tmp/circleci-artifacts 19 | CIRCLE_TEST_REPORTS: /tmp/circleci-test-results 20 | # In CircleCI 1.0 we used a pre-configured image with a large number of languages and other packages. 21 | # In CircleCI 2.0 you can now specify your own image, or use one of our pre-configured images. 22 | # The following configuration line tells CircleCI to use the specified docker image as the runtime environment for you job. 23 | # We have selected a pre-built image that mirrors the build environment we use on 24 | # the 1.0 platform, but we recommend you choose an image more tailored to the needs 25 | # of each job. For more information on choosing an image (or alternatively using a 26 | # VM instead of a container) see https://circleci.com/docs/2.0/executor-types/ 27 | # To see the list of pre-built images that CircleCI provides for most common languages see 28 | # https://circleci.com/docs/2.0/circleci-images/ 29 | docker: 30 | - image: circleci/build-image:ubuntu-14.04-XXL-upstart-1189-5614f37 31 | command: /sbin/init 32 | command: /sbin/init 33 | steps: 34 | # Machine Setup 35 | # If you break your build into multiple jobs with workflows, you will probably want to do the parts of this that are relevant in each 36 | # The following `checkout` command checks out your code to your working directory. In 1.0 we did this implicitly. In 2.0 you can choose where in the course of a job your code should be checked out. 37 | - checkout 38 | # Prepare for artifact and test results collection equivalent to how it was done on 1.0. 39 | # In many cases you can simplify this from what is generated here. 40 | # 'See docs on artifact collection here https://circleci.com/docs/2.0/artifacts/' 41 | - run: mkdir -p $CIRCLE_ARTIFACTS $CIRCLE_TEST_REPORTS 42 | # This is based on your 1.0 configuration file or project settings 43 | - run: 44 | working_directory: ~/yyYank/kotlin-rev-solution 45 | environment: 46 | TZ: Asia/Tokyo 47 | command: 'echo ''Asia/Tokyo'' | sudo service mysql restart; sudo service postgresql 48 | restart; ' 49 | - run: 50 | working_directory: ~/yyYank/kotlin-rev-solution 51 | command: sudo rm -rf /var/lib/apt/lists/* 52 | - run: 53 | working_directory: ~/yyYank/kotlin-rev-solution 54 | command: sudo apt upgrade 55 | - run: 56 | working_directory: ~/yyYank/kotlin-rev-solution 57 | command: sudo apt-get update --fix-missing 58 | - run: 59 | working_directory: ~/yyYank/kotlin-rev-solution 60 | command: sudo apt install python3-pip 61 | # Dependencies 62 | # This would typically go in either a build or a build-and-test job when using workflows 63 | # Restore the dependency cache 64 | - restore_cache: 65 | keys: 66 | # This branch if available 67 | - v1-dep-{{ .Branch }}- 68 | # Default branch if not 69 | - v1-dep-master- 70 | # Any branch if there are none on the default branch - this should be unnecessary if you have your default branch configured correctly 71 | - v1-dep- 72 | # This is based on your 1.0 configuration file or project settings 73 | - run: pip install mkdocs==0.16.3 74 | # Save dependency cache 75 | - save_cache: 76 | key: v1-dep-{{ .Branch }}-{{ epoch }} 77 | paths: 78 | # This is a broad list of cache paths to include many possible development environments 79 | # You can probably delete some of these entries 80 | - vendor/bundle 81 | - ~/virtualenvs 82 | - ~/.m2 83 | - ~/.ivy2 84 | - ~/.bundle 85 | - ~/.go_workspace 86 | - ~/.gradle 87 | - ~/.cache/bower 88 | # Test 89 | # This would typically be a build job when using workflows, possibly combined with build 90 | # This is based on your 1.0 configuration file or project settings 91 | - run: echo "-----start test-----" 92 | # This is based on your 1.0 configuration file or project settings 93 | - run: echo "-----no test-----" 94 | # This is based on your 1.0 configuration file or project settings 95 | - run: echo "-----end test-----" 96 | # Deployment 97 | # Your existing circle.yml file contains deployment steps. 98 | # The config translation tool does not support translating deployment steps 99 | # since deployment in CircleCI 2.0 are better handled through workflows. 100 | # See the documentation for more information https://circleci.com/docs/2.0/workflows/ 101 | - run: 102 | working_directory: ~/yyYank/kotlin-rev-solution 103 | command: mkdocs --version 104 | - run: 105 | working_directory: ~/yyYank/kotlin-rev-solution 106 | command: mkdocs build --clean 107 | - run: 108 | working_directory: ~/yyYank/kotlin-rev-solution 109 | command: git config --global user.email yy.yank.me@gmail.com 110 | - run: 111 | working_directory: ~/yyYank/kotlin-rev-solution 112 | command: git config --global user.name yyYank 113 | - run: 114 | working_directory: ~/yyYank/kotlin-rev-solution 115 | command: git commit ./site/* -m "build [ci skip]" 116 | - run: 117 | working_directory: ~/yyYank/kotlin-rev-solution 118 | command: git pull git@github.com:yyYank/kotlin-rev-solution.git master 119 | - run: 120 | working_directory: ~/yyYank/kotlin-rev-solution 121 | command: git push git@github.com:yyYank/kotlin-rev-solution.git master 122 | ## Teardown 123 | ## If you break your build into multiple jobs with workflows, you will probably want to do the parts of this that are relevant in each 124 | ## Save test results 125 | #- store_test_results: 126 | # path: /tmp/circleci-test-results 127 | ## Save artifacts 128 | #- store_artifacts: 129 | # path: /tmp/circleci-artifacts 130 | #- store_artifacts: 131 | # path: build/reports 132 | #- store_artifacts: 133 | # path: /tmp/circleci-test-results 134 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # kotlin-rev-solution 2 | [![Circle CI](https://circleci.com/gh/yyYank/kotlin-rev-solution/tree/master.svg?style=shield)](https://circleci.com/gh/yyYank/kotlin-rev-solution/tree/master) 3 | 4 | This is site' source to search you want to do something by Kotlin. 5 | It's written in Japanese. 6 | 7 | 逆引きKotlinのサイトのソースです 8 | 9 | 10 | 11 | # サイト 12 | 13 | 14 | Site is below: 15 | ここから見れます 16 | 17 | 逆引きKotlin 18 | 19 | 20 | 21 | ## プルリクについてのお願い 22 | 23 | プルリク募集してます!ぜひ! 24 | 25 | 以下の事項を踏まえた上でお送りいただけると嬉しいです。 26 | 27 | 28 | * site配下のhtmlのプルリクは受け付けないです(mdから生成されているため) 29 | * mkdocs buildとmkdocs serveで動作確認済みのものである 30 | * 基本的なことですが1つのトピック単位のリクエスト等が望ましいです(例えば、「関数を宣言」するのmd編集と関連するリンクの編集など) 31 | 32 | ※本サイトはMkdocsで動作しています 33 | 34 | http://www.mkdocs.org 35 | 36 | 37 | ## サイトの動作環境 38 | 39 | herokuを利用しています。 40 | 41 | https://www.heroku.com 42 | 43 | 44 | プロジェクトにビルドされたhtmlを含めてGitHub上で管理しているのは、GitHubのこのリポジトリとherokuを連携させているためです。 45 | (もしかしたらheroku側にmkdocs build mkdocs serve相当のことを起動スクリプトとして埋め込むことも出きるかもしれません。方法が分かれば改善します。) 46 | 47 | 48 | License 49 | -------------------------------------------------- 50 | 51 | * [Apache License Version 2.0](http://www.apache.org/licenses/LICENSE-2.0) 52 | -------------------------------------------------------------------------------- /docs/all-open-plugin.md: -------------------------------------------------------------------------------- 1 | 2 | # SpringのAOPやMockitoでopenって書きたくない 3 | 4 | all openプラグインを利用します。 5 | 6 | ## 利用できるversion 7 | 8 | 1.1以降 9 | 10 | 11 | ## 使い方 12 | 13 | build.gradleに以下を追記してください。 14 | 明示的なopenが不要になり、デフォルトでclassがopen(継承可能)となります。 15 | 16 | ``` 17 | buildscript { 18 | dependencies { 19 | classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version" 20 | } 21 | } 22 | 23 | apply plugin: "kotlin-allopen" 24 | 25 | allOpen { 26 | annotation("com.your.Annotation") 27 | } 28 | ``` 29 | 30 | ## 用途 31 | 32 | DIライブラリ、Mockitoなどとの組み合わせ 33 | 34 | ## 参考URL 35 | 36 | * [https://blog.jetbrains.com/kotlin/2016/12/kotlin-1-0-6-is-here/](https://blog.jetbrains.com/kotlin/2016/12/kotlin-1-0-6-is-here/) 37 | * [http://taro.hatenablog.jp/entry/2016/12/28/105949](http://taro.hatenablog.jp/entry/2016/12/28/105949) 38 | * [http://stackoverflow.com/questions/41462180/kotlin-all-open-compiler-plugin-doesnt-work](http://stackoverflow.com/questions/41462180/kotlin-all-open-compiler-plugin-doesnt-work) 39 | -------------------------------------------------------------------------------- /docs/array.md: -------------------------------------------------------------------------------- 1 | ## 配列を生成する 2 | 3 | 4 | 配列の利用は、Arrayクラスのインスタンスを生成します。 5 | 6 | 7 | ### フォーマット 8 | 9 | 説明をとても省略して簡単に言うと、以下のフォーマットで 10 | 配列を作成します。 11 | 12 | Array<型名>(要素の数, {初期化処理}) 13 | 14 | 15 | ### 実装サンプル 16 | 17 | 実際には以下のように宣言します。 18 | (偶数を宣言しています) 19 | 20 | // [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] 21 | val evenNumbers = Array(10, {it * 2}) 22 | 23 | 24 | ### array, listOf, arrayListOf, linkedListOf 25 | 26 | 配列の宣言にはKotlinの標準ライブラリとして以下の関数が提供されています。 27 | 28 | 29 | 30 | 31 | 32 | #### arrayOf 33 | Arrayインスタンスを簡易に生成 34 | #### listOf 35 | java.util.Listを生成。要素を変更できません。 36 | #### arrayListOf 37 | java.util.ArrayListインスタンスを生成。Javaと同様に追加、削除などが可能。 38 | #### linkedListOf 39 | java.util.LinkedListインスタンスを生成。Javaと同様に追加、削除などが可能。 40 | 41 | arrayListという関数もあったようですが、非推奨になっています。 42 | 43 | 44 | 45 | 46 | ### 実装サンプル 47 | val oddNumbers = arrayOf(1, 3, 5, 7, 9) 48 | val list = listOf(1,2,3,4,5) 49 | val arrayList = arrayListOf(1,2,3,4,5) 50 | val linkedList = linkedListOf(1,2,3,4,5) 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 |
60 |
61 |
62 |
63 |
64 | 65 | -------------------------------------------------------------------------------- /docs/collection-drop.md: -------------------------------------------------------------------------------- 1 | 2 | ## リストの要素を前からいくつか削る 3 | 4 | drop関数を使います 5 | 6 | ### フォーマット 7 | 8 | drop(削る要素数) 9 | 10 | ### 実装サンプル 11 | 12 | val array = arrayOf(1, 2, 3, 4, 5) 13 | array.drop(1).forEach{println(it)} // => 2, 3, 4, 5 14 | array.drop(2).forEach{println(it)} // => 3, 4, 5 15 | array.drop(3).forEach{println(it)} // => 4, 5 16 | array.drop(4).forEach{println(it)} // => 5 17 | array.drop(5).forEach{println(it)} // => 空のArray 18 | 19 | 20 | ### 参考URL 21 | 22 | [Kotlin公式ドキュメント stdlib / kotlin /drop](http://kotlinlang.org/api/latest/jvm/stdlib/kotlin/drop.html) 23 | -------------------------------------------------------------------------------- /docs/collection-filter.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## 条件でリストの要素を絞り込む 4 | 5 | 6 | filter関数を使います 7 | 8 | ### フォーマット 9 | 10 | filter { 条件 } 11 | 12 | ### 実装サンプル 13 | 14 | val array = arrayOf(1, 2, 3, 4, 5) // => 1,2,3,4,5 のArrayの生成 15 | // filterの中に条件となる関数を渡します 16 | array.filter({e -> e % 2 == 0}).forEach{ println(it) } // => 2,4 17 | // ()は省略出来ます 18 | array.filter{e -> e % 2 == 0}.forEach{ println(it) } // => 2,4 19 | // ラムダで書かなくてもitで代用することも可能です 20 | array.filter{it % 2 == 0}.forEach{ println(it) } // => 2,4 21 | 22 | ### 参考URL 23 | 24 | [Kotlin公式ドキュメント stdlib / kotlin / filter](http://kotlinlang.org/api/latest/jvm/stdlib/kotlin/filter.html) 25 | -------------------------------------------------------------------------------- /docs/collection-for-each.md: -------------------------------------------------------------------------------- 1 | ## リストの要素をループして操作する 2 | 3 | forEach関数を使います 4 | 5 | ### フォーマット 6 | 7 | forEach{ 処理 } 8 | 9 | ### 実装サンプル 10 | 11 | val array = arrayOf(1, 2, 3, 4, 5) 12 | 13 | // ラムダを使った書き方 14 | array.forEach({ element -> 15 | println(element) 16 | }) // => 1,2,3,4,5 17 | 18 | // ()は省略できます 19 | array.forEach{ element -> 20 | println(element) 21 | } // => 1,2,3,4,5 22 | 23 | // itでラムダの変わりに要素を評価することも可能 24 | array.forEach{ 25 | println(it) 26 | } // => 1,2,3,4,5 27 | 28 | 29 | ### 参考URL 30 | 31 | [Kotlin公式ドキュメント stdlib / kotlin / for-each](http://kotlinlang.org/api/latest/jvm/stdlib/kotlin/for-each.html) 32 | -------------------------------------------------------------------------------- /docs/collection-map.md: -------------------------------------------------------------------------------- 1 | ## リストの要素の名前のみを抽出する 2 | 3 | map関数を使います 4 | 5 | ### フォーマット 6 | 7 | map { 値 } 8 | 9 | ### 実装サンプル 10 | 11 | // 名前と年齢を持つだけのクラスを定義 12 | class Person(val name : String, val age : Int) 13 | 14 | fun main (args : Array) { 15 | val array = arrayOf( 16 | Person("kimura", birthDayToAge(LocalDate.of(1972,11,13))), 17 | Person("kusanagi", birthDayToAge(LocalDate.of(1974,7,9))), 18 | Person("katori", birthDayToAge(LocalDate.of(1977,1,31))), 19 | Person("inagaki", birthDayToAge(LocalDate.of(1973,12,8))), 20 | Person("nakai", birthDayToAge(LocalDate.of(1972,8,18))) 21 | ) 22 | // lambdaでuserインスタンスを引数とします 23 | array.map({user -> user.name}).forEach{println(it)}  // kimura,kusanagi,katori,inagaki,nakai 24 | // ()は省略できます 25 | array.map{user -> user.name}.forEach{println(it)}  // kimura,kusanagi,katori,inagaki,nakai 26 | // itで代用できます(このitはUserのインスタンス) 27 | array.map{it.name}.forEach{println(it)}  // kimura,kusanagi,katori,inagaki,nakai 28 | } 29 | 30 | fun birthDayToAge(birthday : LocalDate) = ChronoUnit.YEARS.between(birthday, LocalDate.now()) 31 | 32 | 33 | 第一線で活躍する40代 34 | 35 | 36 | ### 参考URL 37 | 38 | [Kotlin公式ドキュメント stdlib / kotlin / map](http://kotlinlang.org/api/latest/jvm/stdlib/kotlin/map.html) 39 | 40 | 41 | -------------------------------------------------------------------------------- /docs/collection-merge.md: -------------------------------------------------------------------------------- 1 | 2 | ## リストをマージする 3 | 4 | merge関数を使います 5 | 6 | ### フォーマット 7 | 8 | マージする対象のリストA.merge(マージする対象のリストB){戻り値} 9 | 10 | ### 実装サンプル 11 | 12 | // 年齢と名前を持つクラス 13 | class Person(val name : String, val age : Int) 14 | 15 | fun main (args : Array) { 16 | 17 | // なんでもこなすグループ 18 | val sGroup = arrayOf( 19 | Person("kimura", birthDayToAge(LocalDate.of(1972,11,13))), 20 | Person("kusanagi", birthDayToAge(LocalDate.of(1974,7,9))), 21 | Person("katori", birthDayToAge(LocalDate.of(1977,1,31))), 22 | Person("inagaki", birthDayToAge(LocalDate.of(1973,12,8))), 23 | Person("nakai", birthDayToAge(LocalDate.of(1972,8,18))) 24 | ) 25 | 26 | // なんでも作るグループ 27 | val tGroup = arrayOf( 28 | Person("nagase", birthDayToAge(LocalDate.of(1978,11,7))), 29 | Person("joushima", birthDayToAge(LocalDate.of(1970,11,17))), 30 | Person("matsuoka", birthDayToAge(LocalDate.of(1977,1,11))), 31 | Person("yamaguchi", birthDayToAge(LocalDate.of(1972,1,10))), 32 | Person("kokubun", birthDayToAge(LocalDate.of(1974,9,2))) 33 | ) 34 | 35 | // 年齢の高い方をマージする 36 | kGroup 37 | .merge(tGroup){ 38 | k,t -> if (k.age > t.age) k else t // 年上のPersonを選択 39 | }.forEach{ 40 | println(it.name) 41 | } // => kimura,joushima,matsuoka,yamaguchi,nakai 42 | // なかなか見ないキャスティングになった 43 | } 44 | 45 | fun birthDayToAge(birthday : LocalDate) = ChronoUnit.YEARS.between(birthday, LocalDate.now()) 46 | 47 | ### 参考URL 48 | 49 | [Kotlin公式ドキュメント stdlib / kotlin / merge](http://kotlinlang.org/api/latest/jvm/stdlib/kotlin/merge.html) 50 | 51 | 52 | -------------------------------------------------------------------------------- /docs/collection-sum.md: -------------------------------------------------------------------------------- 1 | 2 | ## リストの値を集計する 3 | 4 | sum関数を使います 5 | 6 | ### フォーマット 7 | 8 | 対象のリスト.sum() 9 | 10 | ### 実装サンプル 11 | 12 | // Arrayのインスタンス生成 13 | val array = arrayOf(1,2,3,4,5) 14 | 15 | // sumする(1 + 2 + 3 + 4 + 5) 16 | println(array.sum()) // => 15 17 | 18 | ### 参考URL 19 | 20 | [Kotlin公式ドキュメント stdlib / kotlin / sum](http://kotlinlang.org/api/latest/jvm/stdlib/kotlin/sum.html) 21 | -------------------------------------------------------------------------------- /docs/collection-take.md: -------------------------------------------------------------------------------- 1 | 2 | ## リストの要素を前からいくつか取り出す 3 | 4 | take関数を使います 5 | 6 | ### フォーマット 7 | 8 | take(取り出す要素数) 9 | 10 | ### 実装サンプル 11 | 12 | val array = arrayOf(1, 2, 3, 4, 5) 13 | array.take(1).forEach { println(it) } // => 1 14 | array.take(2).forEach { println(it) } // => 1, 2 15 | array.take(3).forEach { println(it) } // => 1, 2, 3 16 | array.take(4).forEach { println(it) } // => 1, 2, 3, 4 17 | array.take(5).forEach { println(it) } // => 1, 2, 3, 4, 5 18 | 19 | 20 | ### 参考URL 21 | 22 | [Kotlin公式ドキュメント stdlib / kotlin /take](http://kotlinlang.org/api/latest/jvm/stdlib/kotlin/take.html) 23 | -------------------------------------------------------------------------------- /docs/collection-zip.md: -------------------------------------------------------------------------------- 1 | ## 2つのリストのペアのリストを作る 2 | 3 | zip関数を使います 4 | 5 | ### フォーマット 6 | 7 | ### 実装サンプル 8 | 9 | 10 | 分かりやすく、2つのグループで2人1組になってもらいます。 11 | 12 | // 年齢と名前を持ったクラス 13 | class Person(val name : String, val age : Int) 14 | 15 | fun main (args : Array) { 16 | // なんでもこなすグループ 17 | val sGroup = arrayOf( 18 | Person("kimura", birthDayToAge(LocalDate.of(1972,11,13))), 19 | Person("kusanagi", birthDayToAge(LocalDate.of(1974,7,9))), 20 | Person("katori", birthDayToAge(LocalDate.of(1977,1,31))), 21 | Person("inagaki", birthDayToAge(LocalDate.of(1973,12,8))), 22 | Person("nakai", birthDayToAge(LocalDate.of(1972,8,18))) 23 | ) 24 | 25 | // なんでも作るグループ 26 | val tGroup = arrayOf( 27 | Person("nagase", birthDayToAge(LocalDate.of(1978,11,7))), 28 | Person("joushima", birthDayToAge(LocalDate.of(1970,11,17))), 29 | Person("matsuoka", birthDayToAge(LocalDate.of(1977,1,11))), 30 | Person("yamaguchi", birthDayToAge(LocalDate.of(1972,1,10))), 31 | Person("kokubun", birthDayToAge(LocalDate.of(1974,9,2))) 32 | ) 33 | 34 | // 2グループのそれぞれの人の名前を出力 35 | sGroup.zip(tGroup).forEach{println( 36 | it.first.name + "と" + it.second.name 37 | )} 38 | 39 | // 一般的にはこういう書き方をします 40 | sGroup.zip(tGroup).forEach{println( 41 | "${it.first.name}と${it.second.name}" 42 | )} 43 | } 44 | 45 | fun birthDayToAge(birthday : LocalDate) = ChronoUnit.YEARS.between(birthday, LocalDate.now()) 46 | 47 | 48 | 出力結果 49 | 50 | kimuraとnagase 51 | kusanagiとjoushima 52 | katoriとmatsuoka 53 | inagakiとyamaguchi 54 | nakaiとkokubun 55 | 56 | 57 | 58 | ### 参考URL 59 | 60 | 61 | [Kotlin公式ドキュメント stdlib / kotlin / zip](http://kotlinlang.org/api/latest/jvm/stdlib/kotlin/zip.html) 62 | 63 | -------------------------------------------------------------------------------- /docs/coroutines-for-kotlin.md: -------------------------------------------------------------------------------- 1 | 2 | # コールバックヘルとサヨナラしたい 3 | 4 | 5 | coroutinesを使うと良いかもしれません。 6 | 7 | 8 | ## 導入されたversion 9 | 10 | 1.1(experimental) 11 | 12 | 1.2で非同期用のAPIが追加されるが、async/awaitの文法上の変更は無いとのこと。 13 | 14 | 15 | ## サンプルコード 16 | 17 | ```kotlin 18 | future { 19 | val original = asyncLoadImage("...original...") // creates a Future 20 | val overlay = asyncLoadImage("...overlay...") // creates a Future 21 | ... 22 | // suspend while awaiting the loading of the images 23 | // then run `applyOverlay(...)` when they are both loaded 24 | return applyOverlay(original.await(), overlay.await()) 25 | } 26 | ``` 27 | 28 | 29 | ```kotlin 30 | // inferred type is Sequence 31 | val fibonacci = buildSequence { 32 | yield(1) // first Fibonacci number 33 | var cur = 1 34 | var next = 1 35 | while (true) { 36 | yield(next) // next Fibonacci number 37 | val tmp = cur + next 38 | cur = next 39 | next = tmp 40 | } 41 | } 42 | ``` 43 | 44 | 45 | ## 環境設定 46 | 47 | * コマンドライン: -Xcoroutines=enabled オプションをつける 48 | * Gradle: gradle.properties or local.properties で kotlin.coroutines=enableを設定 49 | * Maven: -Xcoroutines=enable を設定 50 | * IDE: quick-fix (Alt+Enter)かfacet optionsで編集可能(Project Structure -> Modules -> Your Module -> Compiler -> Coroutines (experimental)) 51 | 52 | ## 参考URL 53 | 54 | * [https://www.youtube.com/watch?v=4W3ruTWUhpw](https://www.youtube.com/watch?v=4W3ruTWUhpw) 55 | * [https://blog.jetbrains.com/kotlin/2017/01/kotlin-1-1-beta-is-here/](https://blog.jetbrains.com/kotlin/2017/01/kotlin-1-1-beta-is-here/) 56 | * [https://github.com/Kotlin/kotlin-coroutines](https://github.com/Kotlin/kotlin-coroutines) 57 | * [https://github.com/Kotlin/kotlin-coroutines/blob/master/kotlin-coroutines-informal.md](https://github.com/Kotlin/kotlin-coroutines/blob/master/kotlin-coroutines-informal.md) 58 | -------------------------------------------------------------------------------- /docs/enum.md: -------------------------------------------------------------------------------- 1 | 2 | # enumを使いたい 3 | 4 | Javaと同等のenumがあります。 5 | 6 | [Enum Classes](https://kotlinlang.org/docs/reference/enum-classes.html) 7 | 8 | enum class Direction { 9 | NORTH, SOUTH, WEST, EAST 10 | } 11 | -------------------------------------------------------------------------------- /docs/extension.md: -------------------------------------------------------------------------------- 1 | #既存のクラスに機能を追加する 2 | 3 | KotlinではC#のように既存クラスのソースに変更を行うことなく、機能を追加することができます。 4 | 5 | ##拡張関数 6 | 7 | 既存のクラスに関数を追加するには、以下のように書きます。 8 | 9 | class User(var name = "") 10 | 11 | // Userクラスに関数を追加 12 | fun User.isEmptyName() = this.name == "" // thisはUserのオブジェクトを参照します 13 | 14 | ##拡張プロパティ 15 | 16 | 既存のクラスにプロパティを追加するには、以下のように書きます。 17 | 18 | // さん付けの名前プロパティを持たせる 19 | val User.appendedName : String 20 | get() = name.toString() + "さん" 21 | 22 | 拡張プロパティで注意しなければならないのは、実際にクラスにはメンバーを挿入していない点です。 23 | バッキングフィールドを持てないため、初期化子を利用することは許可されていません。 24 | getter/setterでのみ値の決定が可能です。 25 | 26 | 27 | 28 | ####参考 29 | 30 | [Kotlin Reference - Extensions](http://kotlinlang.org/docs/reference/extensions.html) 31 | 32 | -------------------------------------------------------------------------------- /docs/framework-for-kotlin.md: -------------------------------------------------------------------------------- 1 | 2 | # Kotlinでフレームワークを使いたい 3 | 4 | karaというフレームワークもありますが一旦忘れましょう。 5 | 下記の2つのフレームワークが筆者の個人的なおすすめです。 6 | 全部入りのフレームワークを使いたいならSpringの恩恵が受けられるSpring Boot、 7 | 薄いフレームワークで自分で各機能を作っていきたいならwasabi、といった感じでしょうか。 8 | 9 | 10 | 11 | ## Spring Boot 12 | 13 | 14 | ### サンプル 15 | 16 | 公式サイトに載っています。 17 | 18 | [Creating a RESTful Web Service with Spring Boot](https://kotlinlang.org/docs/tutorials/spring-boot-restful.html) 19 | 20 | ### Gradle 21 | 22 | 23 | dependencies { 24 | compile("org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version") // New 25 | compile("org.springframework.boot:spring-boot-starter-web") 26 | testCompile("junit:junit") 27 | } 28 | 29 | ### Maven 30 | 31 | 32 | 33 | 34 | org.jetbrains.kotlin 35 | kotlin-stdlib 36 | 1.0.0 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-starter-web 41 | 1.3.3.RELEASE 42 | 43 | 44 | junit 45 | junit 46 | 4.12 47 | 48 | 49 | 50 | ### 参考サイト 51 | 52 | Kotlinのリリースと同じぐらいのタイミングでSpring側がKotlinサポートを発表しています。 53 | 54 | [Developing Spring Boot applications with Kotlin](https://spring.io/blog/2016/02/15/developing-spring-boot-applications-with-kotlin) 55 | 56 | 57 | Spring Bootは2016/02現在の最新バージョンです。コピペする場合には最新バージョンを確認し直してください。 58 | Getting StartedはSpring Initializr使ったほうが楽だと思う。 59 | [Spring Initializr](https://start.spring.io/#!language=kotlin) 60 | 61 | 62 | 63 | ### エントリポイント(アプリ実行) 64 | 65 | @SpringBootApplication 66 | open class Application { 67 | companion object { 68 | @JvmStatic public fun main(args: Array) { 69 | SpringApplication.run(Application::class.java, *args) 70 | } 71 | } 72 | } 73 | 74 | このクラスをメインクラスに指定した上で、実行してlocalhost:8080にアクセスすれば 75 | 画面が表示されます。 76 | Spring Bootすごい。 77 | 78 | 79 | 80 | ## wasabi 81 | 82 | 83 | wasabiはフルKotlinなHTTPフレームワーク。 84 | 主に、httpリクエストのルーティングをコントロールする。 85 | https://github.com/hhariri/wasabi 86 | 87 | 88 | ## Maven 89 | 90 | 91 | ``` 92 | 93 | org.wasabifx 94 | wasabi 95 | 0.3.120 96 | pom 97 | 98 | ``` 99 | 100 | ## Gradle 101 | 102 | ``` 103 |    dependencies { 104 | // Other dependencies you use. 105 | compile 'org.wasabifx:wasabi:0.3.120' 106 | } 107 | ``` 108 | 109 | 110 | ## コード 111 | 112 | こんな感じで書くと、ルーティングが出来る。 113 | server.start()で組み込みJettyが起動する。 114 | 115 | var server = AppServer() 116 | 117 | server.get("/", { response.send("Hello World!") }) 118 | 119 | server.start() 120 | 121 | -------------------------------------------------------------------------------- /docs/function.md: -------------------------------------------------------------------------------- 1 | ## 関数を宣言する 2 | 3 | 関数の宣言はfunを使います。 4 | 関数はクラスに属す必要はありません。トップレベルに定義できます。 5 | (クラスの関数としても宣言できます) 6 | 7 | 8 | ### フォーマット 9 | 10 | 11 | 12 | 13 | fun 関数名(仮引数 : 型) : 型 = 処理 14 | fun 関数名(仮引数 : 型) : 型 { 15 | 処理 16 | } 17 | 18 | 19 | 20 | ### 実装サンプル 21 | 22 | 23 | 以下のように実装できます。 24 | 25 | 26 | 27 | 28 | fun greetPerson() : String = "yank" 29 | 30 | fun greet() : Unit { 31 | println("hello") 32 | } 33 | 34 | fun greetTo(person : String) : Unit = println("hello! $person") 35 | 36 | 37 | さらに推論される型は省略できます。 38 | 39 | fun greetPerson() = "yank" 40 | 41 | fun greet() { 42 | println("hello") 43 | } 44 | 45 | fun greetTo(person : String) = println("hello! $person") 46 | 47 | 48 | 49 | 50 | $personは文字列への埋め込みの記法です。 51 | string templateまたはstring interpolationと一般に呼ばれます。 52 | -------------------------------------------------------------------------------- /docs/function_object.md: -------------------------------------------------------------------------------- 1 | # 関数をオブジェクトとして扱う 2 | 3 | Kotlinでは関数をオブジェクトとして扱うことができます。 4 | 5 | これによって、引数として受け取ったり、戻り値として返すことができます。 6 | 7 | 関数をオブジェクトとして扱う方法は3通りあります。 8 | 9 | * 既に存在する関数から関数オブジェクトを取得する 10 | * 関数リテラルで直接関数オブジェクトを作成する 11 | * 関数式で直接関数オブジェクトを作成する 12 | 13 | 14 | ## フォーマット 15 | 16 | 既存関数から関数オブジェクトを取得する場合 17 | 18 | ::関数名 19 | 20 | 関数リテラルで定義する場合 21 | 22 | { 引数 [, 引数]... -> 処理 } 23 | 24 | 関数式で定義する場合 25 | 26 | fun(引数[, 引数]...) = 式 27 | 28 | 関数リテラル、関数式の場合は、引数、戻り値の型を指定して定義することもできます。 29 | 30 | { (引数 : 型[, 引数 : 型]...) -> 処理 } 31 | 32 | fun(引数: 型[, 引数: 型]...): 型 = 式 33 | 34 | ## 実装サンプル 35 | 36 | 配列のフィルタ条件を関数オブジェクトとして定義する場合 37 | 38 | val array = arrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) 39 | 40 | fun condition(x: Int): Boolean { 41 | return x % 2 == 0 42 | } 43 | 44 | val even = array.filter(::condition) // 既存関数から関数オブジェクト取得(偶数を抽出) 45 | 46 | val odd = array.filter({x -> x % 2 == 1}) // 関数リテラルで指定(奇数を抽出) 47 | 48 | val small = array.filter(fun(x) = x <= 5) // 関数式で指定(5以下を抽出) 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /docs/function_type.md: -------------------------------------------------------------------------------- 1 | # 好きなタイミングで特定の処理を行う 2 |
3 | 4 | そういうとき、高階関数が便利です。 5 | 6 | [高階関数 - Wikipedia](https://ja.wikipedia.org/wiki/%E9%AB%98%E9%9A%8E%E9%96%A2%E6%95%B0) 7 | 8 | 9 | 10 |
11 | ## フォーマット 12 |
13 |
14 | 関数型の指定は、以下のように書きます。 15 | 16 | (引数の型[, 引数の型]...) -> 戻り値の型 17 | 18 |
19 |
20 | ## 使用例 21 |
22 |
23 | #### 関数を引数として渡す 24 |
25 | 26 | 関数を引数として渡せば、振る舞いの定義だけすることが出来ます。 27 | 28 | Ajaxのコールバックなどが好例です。 29 | 30 | 31 | 引数として関数オブジェクトを受け取るには、次のように書きます。 32 | 33 | fun printResult(callee: (Int, Int) -> Int, val1: Int, val2: Int): String { 34 | val result = callee(val1, val2) 35 | return "Result : $result"; 36 | } 37 | 38 | この例では、第一引数に関数を受け取るように定義しています。 39 | 受け取る関数は、第一、第二引数、戻り値ともにInt型を指定しています。 40 | 41 |
42 |
43 | #### 関数を戻り値として受け取る 44 |
45 |
46 | 逆に関数オブジェクトを戻り値として受け取れば、好きなタイミングで 47 | 実行できます。 48 | 49 | fun example2(val1: Int): (Int, Int) -> Int { 50 | return {x, y -> (x + y) % val1}; 51 | } 52 | 53 | 関数型には引数で受け取る場合と同じ型を指定しています。 54 | 55 | 返す関数は関数リテラルで、クロージャーを使用して引数として渡された値を使用するように書いています。 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /docs/gradle-kts.md: -------------------------------------------------------------------------------- 1 | 2 | ## GradleのビルドスクリプトとしてKotlinを使う 3 | 4 | 5 | Gradle3.0移行、GradleのビルドスクリプトをKotlinで書くことが可能になりました。 6 | 7 | 8 | 2016年12月現在build.gradle.ktsというスクリプトファイルを用意することでビルドスクリプトをKotlinで記述可能です。 9 | 以下のように記述します。 10 | 11 | ```kotlin 12 | import org.gradle.api.plugins.* 13 | import org.gradle.api.tasks.wrapper.* 14 | import org.gradle.script.lang.kotlin.* 15 | 16 | group = "gradle-meets-kotlin" 17 | version = "1.0.0-SNAPSHOT" 18 | val kotlinVersion by extra.properties 19 | 20 | apply { 21 | plugin("application") 22 | } 23 | 24 | configure { 25 | mainClassName = "samples.HelloWorld" 26 | applicationName = "gradle-meets-kotlin" 27 | } 28 | 29 | repositories { 30 | jcenter() 31 | mavenCentral() 32 | } 33 | 34 | dependencies { 35 | "testCompile"("junit:junit:4.12") 36 | } 37 | 38 | ``` 39 | 40 | 41 | 42 | settings.gradleの設定も以下のように修正する必要があるようです。 43 | 44 | 45 | ``` 46 | rootProject.buildFileName = 'build.gradle.kts' 47 | 48 | ``` 49 | -------------------------------------------------------------------------------- /docs/hello-kotlin-android.md: -------------------------------------------------------------------------------- 1 | ## AndroidでHello World 2 | 3 | せっかくなのでAndroidのアプリで確かめてみましょう。 4 | 5 | 6 | ### 新規プロジェクトを作成 7 | 8 | 9 | 10 | プロジェクトを新規作成します。Anroidバージョンなどは任意のバージョンにし、 11 | 質問に答えながら作成してください。 12 | ![新規プロジェクト生成](http://3.bp.blogspot.com/-I03KESEJECA/VQGckLDTS5I/AAAAAAAACTA/kDCCVCemlLI/s1600/hellokotlinandroid1.png) 13 | 14 | 15 | 16 | ### KotlinFileを作成 17 | 18 | 右クリック>Kotlin FileもしくはFile>New>Kotlin FileからKotlinのファイルを生成します。 19 | ![Kotlinファイルの生成](http://3.bp.blogspot.com/-aG2xoA01sgw/VQGchBnuN7I/AAAAAAAACSo/kYSXPfWwIjs/s1600/create-koltin-file.png) 20 | 21 | 22 | 23 | ### Javaのソースをコピーすると変換してくれます 24 | 25 | 26 | 27 | JavaのソースをKotlinのファイルにコピーすると下図のようなアラートが出て変換してくれます。小ネタです。 28 | ![Javaのソースの貼付け](http://2.bp.blogspot.com/-n0arOHKJVVU/VQGcgvRlZ0I/AAAAAAAACSg/MsBSTCuMdzw/s1600/Java-paste.png) 29 | ### Kotlinクラス完成 30 | 31 | とりあえずはonCreateを書けば起動できるのでこのように書いてみます。 32 | 33 | 34 | 35 | public class MainActivity : ActionBarActivity { 36 | override fun onCreate(savedInstanceState: Bundle?) { 37 | super.onCreate(savedInstanceState) 38 | setContentView(R.layout.activity_main) 39 | Toast.makeText(this, "Hello Kotlin!!!!", Toast.LENGTH_SHORT).show() 40 | } 41 | } 42 | 43 | 44 | 45 | 46 | 47 | ![Kotlinのコード作成中](http://1.bp.blogspot.com/-huZNU352IeY/VQGcn1Psy_I/AAAAAAAACTQ/2n26DiGtjl0/s1600/kotlinclass.png) 48 | 49 | ### GradleScriptの自動生成 50 | 51 | 右上に「Configure Kotlin」というメッセージが出るので、 52 | as Kotlin(Android with Gradle) moduleというリンクをクリックしてください。 53 | この時Andorid StudioとGradleのKotlinのバージョンにミスマッチがないか注意して選択するようにしてください。 54 | 55 | ![Gradleスクリプトの自動生成1](http://4.bp.blogspot.com/-7Jt30Q7GkPU/VQGchDqYAGI/AAAAAAAACSk/EHIUAB9fbY4/s1600/gradle-alert.png) 56 | 57 | 58 | 59 | Gradleスクリプトが自動生成されます。 60 | 61 | 62 | ![Gradleスクリプトの自動生成2](http://4.bp.blogspot.com/-3ptUTHDgWzs/VQGciqyc0TI/AAAAAAAACS4/0k2Fs2uDmbo/s1600/gradle-genereate.png) 63 | 64 | 65 | ### アプリ起動 66 | 67 | 68 | RunからRun'アプリ名'を選択し、ローンチします。エミュレータか実機などは設定してください。 69 | 70 | ![アプリ起動](http://3.bp.blogspot.com/-xs3VwlVhW2A/VQGcp3ilw-I/AAAAAAAACTY/1vWSZNlpPYk/s1600/%E8%B5%B7%E5%8B%95.png) 71 | 72 | 73 | Hello Kotlin!!!とトーストで表示されれば完了です。 74 | 75 | 76 | -------------------------------------------------------------------------------- /docs/if-function.md: -------------------------------------------------------------------------------- 1 | ## ifを式として扱う 2 | 3 | 4 | Kotlinでのifは単なる条件分岐としての役割だけでなく、 5 | 式として評価するための構文です。 6 | 7 | 8 | val hoge = "hoge" 9 | val result = if(hoge == "hoge") { "期待通り" } else { "hogeじゃない"} 10 | println(result) // => 期待通り 11 | 12 | 13 | 14 | Stringの戻り値を持つ、処理のように扱うことが出来ます。 15 | (もちろん、IntでもBigDecimalでも問題ありません) 16 | 17 | 18 | 19 | このように書くことも出来ます 20 | 21 | 22 | 23 | 24 | 25 | val hoge = "hoge" 26 | val result = 27 | if(hoge == "hoge") "期待通り" 28 | else "hogeじゃない" 29 | println(result) // => 期待通り 30 | 31 | 32 | 33 | 34 | [条件分岐を行う](../if-statement)で書いた処理も 35 | 厳密にはUnitという戻り値を持たない処理ための型を返しています。 36 | 37 | 38 | ### 引用 39 | 40 | 41 | 42 | 43 | > if式 44 | > 式の計算結果の真偽で条件分岐を行う 45 | [AutoHotkeyJp- if式](https://sites.google.com/site/autohotkeyjp/reference/commands/IfExpression) 46 | 47 | 48 | -------------------------------------------------------------------------------- /docs/if-statement.md: -------------------------------------------------------------------------------- 1 | ## 条件分岐を行う 2 | 3 | 4 | 他のプログラミング言語と同様にifを使います。 5 | Kotlinのifは単なる条件分岐ではなく、式(if式)です。 6 | これについては[ifを式として扱う](./if-function)参照。 7 | 8 | その他に[when](../when)などで条件を分岐する方法もあります。 9 | (whenも厳密には式です) 10 | 11 | 12 | ### フォーマット 13 | 14 | if、else if、elseで条件が分岐できます。 15 | 16 | if(条件) { 17 | 処理 18 | } else if(条件){ 19 | 処理 20 | } else { 21 | 処理 22 | } 23 | 24 | 25 | ### 実装サンプル 26 | 27 | val value = 1 28 | 29 | 30 | if(value == 1) { 31 | println("value is one") 32 | } else if(value == 2){ 33 | println("value is two") 34 | } else { 35 | println("other value") 36 | } 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /docs/index.md: -------------------------------------------------------------------------------- 1 | # 逆引きKotlin 2 | 3 |
4 | JVM言語の[Kotlin](http://kotlinlang.org)の逆引きサイトです。![](../images/Kotlin-icon.png) 5 |
6 |
7 | ## 目次 8 |
9 | 10 | ### 導入 11 | * [Kotlinをインストールする](./kotlin-install) 12 | * [KotlinプラグインをIntelliJ IDEAに導入する](./kotlin-plugin-intellij) 13 | * [KotlinプラグインをAndroid Studioに入れる](./kotlin-plugin-androidstudio) 14 | * [AndroidでHello World](./hello-kotlin-android) 15 | * [KotlinをEclipseで動かす](./kotlin-plugin-eclipse) 16 | 17 | ### 基本文法 18 | * [変数を宣言する](./variable) 19 | * [型を宣言する](./type) 20 | * [インスタンスを生成する](./instance) 21 | * [配列を生成する](./array) 22 | * [関数を宣言する](./function) 23 | * [関数をオブジェクトとして扱う](./function_object) 24 | * [好きなタイミングで特定の処理を行う](./function_type) 25 | * [条件分岐を行う](if-statement) 26 | * [ifを式として扱う](./if-function) 27 | * [switchみたいな条件分岐がしたい](./when) 28 | * [ループ処理を行う](./loop) 29 | * [enumを使いたい](./enum) 30 | 31 | ### コレクション操作 32 | * [Kotlinのコレクション・フレームワークを知りたい](./kotlin-collection-framework) 33 | * [リストの要素をループして操作する](./collection-for-each) 34 | * [条件でリストの要素を絞り込む](./collection-filter) 35 | * [リストの要素の名前のみを抽出する](./collection-map) 36 | * [リストをマージする](./collection-merge) 37 | * [リストの値を集計する](./collection-sum) 38 | * [リストの要素を前からいくつか取り出す](./collection-take) 39 | * [リストの要素を前からいくつか削る](./collection-drop) 40 | 41 | 42 | ### 応用編 43 | * [Kotlinでフレームワークを使いたい](./framework-for-kotlin) 44 | * [ユニットテストをしたい](./unit-test-for-kotlin) 45 | * [親子関係のある独自のブロックを作りたい](./type-safe-builder) 46 | * [クラスの機能を拡張したい](./extension) 47 | * [クラスのメタデータを取得したい(リフレクション操作がしたい)](./reflection) 48 | * [Kotlinのキーワードを知りたい](./keyword-for-kotlin) 49 | * [KotlinでSQLライブラリやORM(O/R Mapper)を使いたい](./orm-for-kotlin) 50 | * [Kotlinでtry-with-resourcesが使いたい](./try-with-resources-for-kotlin) 51 | * [TODOタスクをソースに残す](./todo) 52 | * [クラスを引数にとる関数をつくる](./kclass-argument) 53 | * [GradleのビルドスクリプトをKotlinで書く](./gradle-kts) 54 | 55 | ### ver1.1 56 | 57 | * [よく使う型に別名をつけたい](./type-alias) 58 | * [コールバックヘルとサヨナラしたい](./coroutines-for-kotlin) 59 | * [SpringのAOPやMockitoでopenって書きたくない](./all-open-plugin) 60 | 61 |
62 |
63 |
64 | 65 | ## サイト管理者 66 | 67 |
68 | [yy_yank](https://twitter.com/yy_yank) -> Kotlinが好きなだけのプログラマ 69 | 70 |
71 |
72 | 73 | 74 | 75 | ## プルリクお待ちしています 76 |
77 |
78 | 内容の修正や新規追加などのプルリクをお待ちしています。 79 | 80 |
81 | [こちらから](https://github.com/yyYank/kotlin-rev-solution) 82 | 83 |
84 |
85 |
86 |
87 | 88 | 89 | ## 履歴 90 | 91 |
 92 | 2015/03/05:サイト開設
 93 | 2015/03/07:変数の宣言についてページ作成
 94 | 2015/03/08:型の宣言についてページ作成
 95 | 2015/03/08:KotlinをEclipseで動かす ページ作成
 96 | 2015/03/09:Kotlinをインストールする ページ作成
 97 | 2015/03/10:KotlinプラグインをIntelliJ IDEAに導入する ページ作成
 98 | 2015/03/10:インスタンスを生成する ページ作成
 99 | 2015/03/10:配列を生成する ページ作成
100 | 2015/03/11:関数を宣言する ページ作成
101 | 2015/03/11:条件分岐を行う ページ作成
102 | 2015/03/11:ifを式として扱う ページ作成
103 | 2015/03/11:switchみたいな条件分岐がしたい ページ作成
104 | 2015/03/11:ループ処理を行う ページ作成
105 | 2015/03/12:KotlinプラグインをAndroid Studioに入れる ページ作成
106 | 2015/03/12:AndroidでHello World  ページ作成
107 | 2015/07/26:リストの要素をループして操作する ページ作成  
108 | 2015/07/26:条件でリストの要素を絞り込む ページ作成 
109 | 2015/07/26:リストの要素の名前のみを抽出する ページ作成 
110 | 2015/07/26:リストをマージする ページ作成 
111 | 2015/07/26:リストの値を集計する ページ作成 
112 | 2015/07/26:リストの1部分を切り出す] ページ作成 
113 | 2015/07/26:2つのリストのペアのリストを作る  ページ作成 
114 | 2015/08/13:ループ処理を行う  一部修正(drmashuさんプルリク) 
115 | 2015/08/13:クラスの機能を拡張したい  ページ作成(drmashuさんプルリク) 
116 | 2015/08/13:クラスのメタデータを取得したい(リフレクション操作がしたい)  ページ作成(drmashuさんプルリク)
117 | 2015/09/02:関数を宣言する  ページ修正(drmashuさんプルリク)
118 | 2015/09/02:関数をオブジェクトとして扱う  ページ作成(drmashuさんプルリク)
119 | 2015/09/02:好きなタイミングで特定の処理を行う ページ作成(drmashuさんプルリク)
120 | 2015/10/06:リストの要素を前からいくつか取り出す ページ作成(hkdnetさんプルリク)
121 | 2015/10/06:drop関数の説明 リストの要素を前からいくつか削る ページ名変更(hkdnetさんプルリク)
122 | 2015/10/06:Kotlinをインストールする ページ誤字修正(iwata-nさんプルリク)
123 | 2016/1/30:Siteのカスタムテーマ修正(EFEXPさんプルリク)
124 | 2016/2/25:Type-Safe Buildersのページを新規作成
125 | 2016/3/01:ユニットテストのページを新規作成
126 | 2016/3/29:Kotlinのキーワードのページを新規作成
127 | 2016/7/11:KotlinでSQLライブラリやORM(O/R Mapper)を使いたい、Kotlinでtry-with-resourcesが使いたい、TODOタスクをソースに残す、クラスを引数にとる関数をつくるを追加
128 | 2016/12/12:GradleのビルドスクリプトをKotlinで書く新規追加
129 | 2017/1/23:よく使う型に別名をつけたい新規追加
130 | 2017/2/2:コールバックヘルとサヨナラしたい新規追加
131 | 2017/2/12:SpringのAOPやMockitoでopenって書きたくない新規作成
132 | 
133 | 134 | 135 | ## Special Thanks 136 | 137 | ### プルリクしてくれた方 138 | 139 | * [siosioさん](https://github.com/siosio) 140 | * [drmashuさん](https://github.com/drmashu) 141 | * [iwata-nさん](https://github.com/iwata-n) 142 | * [hkdnetさん](https://github.com/hkdnet) 143 | * [EFEXPさん](https://github.com/EFEXP) 144 | * [umireonさん](https://github.com/umireon) 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | -------------------------------------------------------------------------------- /docs/instance.md: -------------------------------------------------------------------------------- 1 | ## インスタンスを生成する 2 | 3 | 4 | 5 | ### フォーマット 6 | 7 | Kotlinにおいてクラスのインスタンスを生成するには 8 | 9 | 10 | クラス名(コンストラクタ引数) 11 | 12 | 13 | の形式で指定します。new演算子は使いません。 14 | 15 | 16 | ### 実装サンプル 17 | 例えば、以下の様なHogeというクラスが有った時 18 | 19 | public class Hoge(val message : String) { 20 | fun printHoge () { 21 | println("helllo! -> $message") 22 | } 23 | } 24 | 25 | インスタンスの生成は以下のように行います。 26 | 27 | val hoge = Hoge("hoge") 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /docs/kclass-argument.md: -------------------------------------------------------------------------------- 1 | 2 | # クラスを引数にとる関数をつくる 3 | 4 | 5 | 引数としてクラスを関数に渡したい場合は以下のように書きます。 6 | 7 | fun jsonToObject(jsonString : String, clazz : KClass) { 8 | // 処理 9 | } 10 | 11 | 12 | Kotlinのクラスは"KClass"です。 13 | この関数を使う側は以下のように書きます。 14 | 15 | 16 | jsonToObject(jsonString, Hoge::class) 17 | 18 | -------------------------------------------------------------------------------- /docs/keyword-for-kotlin.md: -------------------------------------------------------------------------------- 1 | 2 | # Kotlinのキーワードを知りたい 3 | 4 | KotlinではJavaとのcompatibilityを目的にしたものはアノテーション、Kotlin側で使われるものをキーワードと表現する傾向があります。 5 | 6 | キーワードには以下のようなものがあります。 7 | 8 | ## Visibility Modifiers 9 | 10 | キーワードというかアクセス修飾子。 11 | 12 | 13 | [Visibility Modifiers](https://kotlinlang.org/docs/reference/visibility-modifiers.html) 14 | 15 | ### public 16 | 17 | Javaとほぼ同じ。Kotlinではデフォルトのアクセスレベルがpublicとなっている。 18 | 19 | ### private 20 | 21 | Javaとほぼ同じ。クラス内からのみアクセス可能。 22 | 23 | 24 | ### protected 25 | 26 | Javaとほぼ同じ。同一クラスかサブクラスからアクセス可能。 27 | 28 | 29 | ### internal 30 | 31 | 同一モジュール内ならどこでも見える。 32 | 33 | これは、Jigsaw以前のJavaにおいてモジュールという概念無いためピンと来ない人も多いと思う(僕もそう)。 34 | このあたりに書いてあるので参照されたし。 35 | [http://stackoverflow.com/questions/35271413/what-is-a-kotlin-module](http://stackoverflow.com/questions/35271413/what-is-a-kotlin-module) 36 | 要するに同一のコンパイル単位なら参照可能っぽい。同一jarとか考えるとまぁいいかなぁ。 37 | 38 | 39 | 40 | ## classのキーワード 41 | 42 | ### open 43 | 44 | classの継承をする際に親クラスにつける。 45 | Kotlinではデフォルトで継承不可なクラスとなっている。 46 | 47 | open class Hoge 48 | 49 | ### data 50 | 51 | toString、hashCode,equalsなどの基本的な関数を定義したクラスを使いたいときに用いる。data classをつくる時のmodifier 52 | 53 | data class Hoge(val fuga : String) 54 | 55 | 56 | [Data Classes](https://kotlinlang.org/docs/reference/data-classes.html) 57 | 58 | 59 | 60 | ### object 61 | 62 | シングルトンなオブジェクトとか作りたいときに使う。 63 | 64 | 65 | [Object Expressions and Declarations](https://kotlinlang.org/docs/reference/object-declarations.html) 66 | 67 | 68 | ### sealed 69 | 70 | sealed classは厳密に制限された階層構造のクラスを作りたい時に使う。 71 | enumも制限したものを持つ階層構造のものであるが、enumと違うのはサブクラスで扱える点、 72 | 複数のインスタンスを持てる点など。 73 | 74 | [Classes and Inheritance](https://kotlinlang.org/docs/reference/classes.html) 75 | より引用 76 | 77 | 78 | sealed class Expr { 79 | class Const(val number: Double) : Expr() 80 | class Sum(val e1: Expr, val e2: Expr) : Expr() 81 | object NotANumber : Expr() 82 | } 83 | 84 | 85 | 実装サンプル 86 | 87 | 88 | fun main(args: Array) { 89 | val color = Color.Red 90 | val colorName = selectColorName(color) 91 | println(colorName) 92 | } 93 | 94 | fun selectColorName(color : Color) = when(color) { 95 | Color.Red -> "赤" 96 | Color.Green -> "青" 97 | Color.Yellow -> "黄" 98 | } 99 | 100 | 101 | sealed class Color { 102 | object Red : Color() 103 | object Green : Color() 104 | object Yellow : Color() 105 | } 106 | 107 | 108 | when式にelseがないのはsealedのとりうる全ての分岐を網羅している事をコンパイラが認識しているから。 109 | 110 | 参考 111 | [Kotlin M13で追加されたsealed class](http://taro.hatenablog.jp/entry/2015/09/17/131439) 112 | 113 | ## 関数につけるキーワード 114 | 115 | ### inline 116 | 117 | 関数をインライン化することが出来、毎回インスタンス生成するようなオーバーヘッドを避けられる。 118 | inline関数内の特定の箇所をinline化したくない場合はnoinlineを使うようだ。 119 | 120 | [Inline Functions](https://kotlinlang.org/docs/reference/inline-functions.html) 121 | 122 | 123 | 124 | 125 | ### infix 126 | 127 | infixを関数に付与することで中置記法が出来る。 128 | 129 | [Functions](https://kotlinlang.org/docs/reference/functions.html) 130 | 131 | 132 | ### tailrec 133 | 134 | 末尾再起最適化をするためのキーワード。 135 | これでスタックオーバーフローとかしないようになるので安心。 136 | 137 | tailrec fun findFixPoint(x: Double = 1.0): Double 138 | = if (x == Math.cos(x)) x else findFixPoint(Math.cos(x)) 139 | 140 | #### 注意!! 141 | 142 | tailrecはScalaの意味するものと異なる。 143 | Scalaの場合はTailrecアノテーションを 144 | つけなくても末尾再帰最適化されるがKotlinの場合は、 145 | このキーワードをつけない限り末尾再帰最適化されない。 146 | 147 | Scalaはチェックアノテーションになっているらしい。 148 | 149 | ### operator 150 | 151 | 演算子オーバーロードをしたいときに使う。 152 | 153 | ### const 154 | 155 | 定数に使う。ただし、String型かプリミティブ型にしか利用できない。 156 | 157 | const val SUBSYSTEM_DEPRECATED: String = "This subsystem is deprecated" 158 | 159 | ## companion 160 | 161 | staticな関数とかを使いときに使う。companion object。 162 | 163 | class MyClass { 164 | companion object Factory { 165 | fun create(): MyClass = MyClass() 166 | } 167 | } 168 | 169 | 170 | val instance = MyClass.create() 171 | 172 | 173 | 174 | [Object Expressions and Declarations](https://kotlinlang.org/docs/reference/object-declarations.html) 175 | 176 | ## is 177 | 178 | 型チェックなどで使われる。Javaでいうinstanceofが近い。 179 | 180 | ## in 181 | 182 | ジェネリクスで入力のみ可能なコレクションクラスの宣言とかに使う。型パラメータを反変(contravariant)にする。 183 | 184 | ## out 185 | 186 | ジェネリクスで出力のみ可能なコレクションクラスの宣言とかに使う。 187 | 188 | ## vararg 189 | 190 | 引数を可変長としたいときに使う 191 | 192 | ## as 193 | 194 | 別名をつける場合、キャストする場合に利用する。 195 | 196 | import java.math.BigDecimal as Decimal 197 | 198 | 199 | var hoge = Hoge() as Fuga 200 | 201 | ## by 202 | 203 | フィールドのデリゲーションで使う。 204 | 205 | class Example { 206 | var p: String by Delegate() 207 | } 208 | 209 | The syntax is: val/var : by . 210 | 211 | ## to 212 | 213 | toはPairの拡張関数。イディオム的に使える。 214 | 215 | val map = mapOf("a" to 1, "b" to 2) 216 | 217 | toの関数定義はこんな感じ。 218 | 219 | infix fun A.to(that: B): Pair (source) 220 | 221 | [https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/to.html](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/to.html) 222 | 223 | 224 | ## reified 225 | 226 | 読み方はカタカナで書くならリーアファイドとかレイアファイドとか。 227 | javaのクラス指定を少し簡略化してような代物。 228 | 229 | inline fun membersOf() = T::class.members 230 | 231 | 232 | fun main(s: Array) { 233 | println(membersOf().joinToString("\n")) 234 | // こう書かなくて良い println(membersOf().joinToString("\n")) 235 | } 236 | 237 | 238 | たろうさんの記事が詳しい。(M10と現在だとややjavaClassとかあたりは変わっている) 239 | [Kotlin M10で追加されたreified type parameterがいい感じ](http://taro.hatenablog.jp/entry/2015/01/29/223239) 240 | 241 | [Reified type parameters](https://kotlinlang.org/docs/reference/inline-functions.html#reified-type-parameters) 242 | 243 | 244 | ## let,with,run,apply 245 | 246 | スコープ関数と言われる便利なやつ 247 | 248 | [Kotlin スコープ関数 用途まとめ](http://qiita.com/ngsw_taro/items/d29e3080d9fc8a38691e) 249 | 250 | 251 | 252 | hoge.let{} 253 | with(hoge){} 254 | hoge.run{} 255 | hoge.apply{} 256 | 257 | 258 | ## lateinit 259 | 260 | 主に@Injectや@AutowiredなどのDIでフィールドインジェクションをしたい場合に 261 | フィールドにつける。あとはJUnitの@SetUpなど。 262 | 変数はvarにしておかないとダメなのがちょっとやだ。 263 | 264 | lateinit var subject: TestSubject 265 | 266 | 267 | ## constructor 268 | 269 | 複数のコンストラクタを定義したい場合、使う。 270 | このキーワードを使わない場合はプライマリコンストラクタとなる。 271 | 272 | ## init 273 | 274 | 初期化ブロックのキーワード 275 | 276 | 277 | init { 278 | // フィールド初期化などの処理 279 | } 280 | 281 | -------------------------------------------------------------------------------- /docs/kotlin-collection-framework.md: -------------------------------------------------------------------------------- 1 | # Kotlinのコレクション・フレームワークを知りたい 2 | 3 | 4 | Javaにコレクションフレームワークがあるように、Kotlinにもコレクションフレームワークがある。 5 | Kotlinのコレクションフレームワークはread onlyなコレクションとmutable(read/write)なコレクションで構成されている。 6 | immutableではないので注意。 7 | 8 | 代表的なものだけ抜粋。 9 | 10 | # Collection 11 | 12 | kotlin.collection.Collectionインターフェース。 13 | 14 | interface Collection : Iterable 15 | 16 | EはCollectionの要素。このインターフェースはread only のみサポートする。 read/writeアクセスはMutableCollection インターフェースがサポートする。 17 | 18 | 19 | # MutableCollection 20 | 21 | interface MutableCollection : Collection, MutableIterable 22 | 23 | EはCollectionの要素。このインターフェースはread/writeアクセスをサポートする。 24 | 25 | ## List 26 | 27 | Listインターフェース。Collectionインターフェースを継承している。 28 | 29 | interface List : Collection 30 | 31 | EはCollectionの要素。このインターフェースはListのread onlyな操作のみ提供する。 32 | read/writeアクセスはMutableListインターフェースが提供する。 33 | 34 | ## MutableList 35 | 36 | MutableなListインターフェース。 37 | 38 | interface MutableList : List, MutableCollection 39 | 40 | Eはコレクションの要素。要素の追加・削除の機能を提供するインターフェース。 41 | 42 | 43 | ## Map 44 | 45 | interface Map 46 | 47 | ## MutableMap 48 | 49 | interface MutableMap : Map 50 | 51 | 52 | ## Set 53 | 54 | 重複をサポートしない順序性の無いジェネリックなSet。 55 | setへのread onlyな処理を提供するインターフェース。 56 | 57 | interface Set : Collection 58 | 59 | ## MutableSet 60 | 61 | 重複をサポートしない順序性の無いジェネリックなSet。 62 | 要素の追加と削除の処理を提供する。 63 | 64 | interface MutableSet : Set, MutableCollection 65 | 66 | 参考URL 67 | 68 | [https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/) 69 | -------------------------------------------------------------------------------- /docs/kotlin-install.md: -------------------------------------------------------------------------------- 1 | ## Kotlinをインストールする 2 | 3 | 4 | KotlinをIDE(統合開発環境)などを介さずに実行するためには 5 | Kotlin Compilerをダウンロードする必要があります。 6 | 7 | ※導入が面倒ですぐにKotlinを書きたい人はWeb上で確認できます。 8 | 9 | 10 | [try.kotlin.org](http://try.kotlinlang.org/) 11 | 12 | 13 | 14 | 15 | 2015年3月現在、kotlin-compiler-0.10.195.zipとして 16 | ダウンロードできます。 17 | 18 | 19 | Kotlin1.0 is out!!!!! 20 | メジャーバージョン出ました。 21 | 22 | 23 | 24 | [Kotlinの公式サイト](http://kotlinlang.org/)からダウンロードしましょう。 25 | 26 | 27 | なお、IDEを使う場合はこの作業は必要ないです。 28 | 29 | 30 | ### 準備 31 | 32 | 環境変数にKOTLIN_HOME/binを通してください。 33 | 34 | 35 | ### 使えるコマンド 36 | 37 | * kotlinc 38 | * kotlinc-jvm 39 | * kotlinc-js 40 | 41 | kotlincはkoltin-jvmのAlias。つまり、一緒なので 42 | kotlincを使えば間違えないです。 43 | 44 | 45 | 46 | #### kotlinc(kotlinc-jvm) 47 | 48 | 49 | 以下のフォーマットでコマンドは使えます。 50 | 51 | kotlinc <ファイル名> <オプション> 52 | 53 | 具体的には、こんな感じ。 54 | 55 | kotlinc-jvm hello.kt -include-runtime -d hello.jar 56 | 57 | コンパイル時はkotlincですが、実行時はkotlinコマンドはないです。 58 | つまりこういうこと。 59 | 60 | java -jar hello.jar 61 | 62 | -include-runtimeでKotlinの実行ランタイムが内包される。 63 | これがないと動かない。 64 | 65 | 66 | kotlincのオプションは以下のとおり 67 | 68 | 69 | -d jarとして出力するファイルパスを指定 70 | -classpath (-cp) 読み込ませたいclass filesが存在するパスを指定 71 | -annotations external annotationsをパスに含む 72 | -include-runtime Kotlinのランタイムをjarに含む 73 | -no-jdk Java runtimeをクラスパスに含まない 74 | -no-stdlib Kotlin runtimeをクラスパスに含まない 75 | -no-jdk-annotations JDK external annotationsをクラスパスに含まない 76 | -module コンパイルするモジュールを指定 77 | -script Scriptのファイルを評価する 78 | -kotlin-home KotlinランタイムのHOMEを指定(annotationとruntime librariesを見つけるため) 79 | -nowarn 警告を表示しない 80 | -verbose verboseログ出力を有効にする 81 | -version バージョン確認 82 | -help (-h) HELP表示 83 | -X advancedオプション表示 84 | 85 | 86 | 87 | 88 | 89 | kotlincだけ打ち込むとREPL(対話環境モードになる) 90 | 91 | 92 | #### kotlinc-js 93 | 94 | 95 | kolinc-jsにはREPLはないようです。 96 | 動作確認はしていないが以下の様なコマンドで 97 | JavaScriptにコンバートされるよう。 98 | 99 | kotlinc-js -output test -sourceFiles test.kt -libraryFiles kotlin-jslib.jar 100 | 101 | 102 | 103 | オプションは以下のとおり。 104 | 105 | -output 出力ファイルパス 106 | -no-stdlib Kotlinのシステムライブラリを使わない 107 | -library-files zipされたライブラリまたはkotlinファイルをカンマ繋ぎでパス指定 108 | -source-map ソースマップを生成 109 | -target ECMA標準のファイルを生成(ECMA 5のみサポート) 110 | -main {call,noCall} main関数を実行するかどうか(デフォルトは実行) 111 | -output-prefix 出力ファイルにprefixを追加する 112 | -output-postfix 出力ファイルにpostfixを追加する 113 | -nowarn 警告を表示しない 114 | -verbose verboseログ出力を有効にする 115 | -version バージョン確認 116 | -help (-h) HELP表示 117 | -X advancedオプション表示 118 | 119 | 120 | 121 | ### コンパイラ構成 122 | 123 | 124 | #### bin 125 | 126 | * kotlinc 127 | * kotlinc-js.bat 128 | * kotlinc-jvm.bat 129 | * kotlinc-js 130 | * kotlinc-jvm 131 | * kotlinc.bat 132 | 133 | 134 | #### lib 135 | 136 | * kotlin-android-sdk-annotations.jar 137 | * kotlin-jslib.jar 138 | * kotlin-ant.jar 139 | * kotlin-preloader.jar 140 | * kotlin-compiler.jar 141 | * kotlin-runtime-sources.jar 142 | * kotlin-jdk-annotations.jar 143 | * kotlin-runtime.jar 144 | * kotlin-jslib-sources.jar 145 | 146 | 147 | だいたいこんな感じです。 148 | 149 | 150 | 151 | 152 | 参考 153 | 154 | 155 | [公式チュートリアル](http://kotlinlang.org/docs/tutorials/command-line.html) 156 | 157 | [あまり知られていない? #Kotlin の対話型評価環境](http://taro.hatenablog.jp/entry/2013/04/06/162855) 158 | -------------------------------------------------------------------------------- /docs/kotlin-plugin-androidstudio.md: -------------------------------------------------------------------------------- 1 | ## KotlinプラグインをAndroid Studioに入れる 2 | 3 | 4 | 5 | ※追記 6 | 7 | Android Studio 3.0 以降、Kotlinプラグインが標準搭載されるようになったため、 8 | 9 | この手順は不要でKotlinが使用できます!! 10 | 11 | [Android Announces Support for Kotlin](https://android-developers.googleblog.com/2017/05/android-announces-support-for-kotlin.html) 12 | 13 | 14 | ### 事前準備 15 | 16 | Android Studioのインストール 17 | 18 | 19 | ### プラグイン導入手順 20 | 21 | 22 | [KotlinプラグインをIntelliJ IDEAに導入する](../kotlin-plugin-intellij)と手順は同じです。 23 | 24 | 25 | 26 | * Android Studio起動 27 | * Preferencesを開く(Ctrl(cmd) + カンマもしくはAndroid Studio>Preferences) 28 | ![1](http://4.bp.blogspot.com/-zOzf7VPr-1o/VQGMKCpds3I/AAAAAAAACSE/9dJOs-Hihy0/s1600/1.png) 29 | * Pluginsを選択する 30 | ![2](http://4.bp.blogspot.com/-idvhEx1Qw3w/VQGMJ58-d-I/AAAAAAAACSA/g2UgqCSoiP8/s1600/2.png) 31 | * Install JetBrains Plugin...を選択する 32 | * Kotlinを選択しinstall 33 | ![3](http://2.bp.blogspot.com/-WybLxsvoaQM/VQGMKN29HzI/AAAAAAAACSI/R4VlSgnGXtI/s1600/3.png) 34 | * Android Studioを再起動 35 | これでプラグイン導入は完了です。 36 | 37 | 38 | 39 | ### Hello World 40 | 41 | せっかくなのでAndroidのアプリで確かめてみましょう。 42 | と思いましたが、長くなるので、別ページに記載します。 43 | 44 | 45 | [AndroidでHello World](./hello-kotlin-android/) 46 | 47 | 48 | -------------------------------------------------------------------------------- /docs/kotlin-plugin-eclipse.md: -------------------------------------------------------------------------------- 1 | ## KotlinをEclipseで動かす 2 | 3 | 4 | ### 手順 5 | 6 | こちらの公式ブログに詳しく載っています。 7 | (Eclipse Plugin Alpha is Out!)[http://blog.jetbrains.com/kotlin/2015/03/eclipse-plugin-alpha-is-out/] 8 | 9 | 1.Eclipseを起動し、「Help(ヘルプ)>Eclipse Market Place(マーケットプレイス)」を選択して 10 | 11 | Kotlinで検索しプラグインをインストール 12 | 13 | 14 | もしくは「Help(ヘルプ)> Install New Software(新規ソフトウェアのインストール)」を選択し、 15 | 16 | 17 | https://dl.bintray.com/jetbrains/kotlin/eclipse-plugin/last/へアクセスする 18 | 19 | 20 | 2.ライセンスに同意します 21 | 22 | 23 | 3.Eclipseを再起動します 24 | 25 | 26 | 27 | 以上 28 | 29 | 30 | 31 | 32 | 33 | 34 | ここから下は昔話です 35 | 36 | 37 | Kotlinの公式サイトからは現在公表されておらず、 38 | (昔はcoming soonとか書いてあった) 39 | 40 | プラグインは公式リリースされているものではありません(2015年3月8日現在)。 41 | 42 | 43 | 44 | 素直にIntelliJ IDEAかAndroid Studio使ったほうが楽です。 45 | 一応記録として残しておく程度という認識でお願いします。 46 | 47 | ### 事前準備 48 | * Eclipse 4.3.1(Kepler)以上インストール 49 | -> Kepler推奨みたいですが、Lunaでも動きました。 50 | * Eclipse SDKインストール 51 | * [kotlin-eclipse](https://github.com/JetBrains/kotlin-eclipse)をgit cloneする 52 | -> Jet Brains社提供のEclipse SDKのKotlin開発環境 53 | 54 | ### 手順 55 | 56 | 1.Eclipseを起動し、File>Import>Project>existing project into workspaceで 57 | kotlin-eclipseのプロジェクトを取り込む 58 | 59 | 2.kotlin-eclipseプロジェクトのantスクリプトからkotlin-eclipseをビルドする 60 | 61 | 62 | 3.Kotlin-eclipseのlauncherプロジェクトのlaunch用のantスクリプトを実行 63 | 64 | 65 | 66 | 3.の実行時にプラグインを全て追加する必要があります。 67 | うまく行けばEclipse上にEclipse(Eclipse SDK)が起動するような形になる。 68 | 69 | 70 | ### 使ってみた感想 71 | 72 | Eclipseの良さがあんまり感じられなくなっちゃってる 73 | 74 | * Eclipse起動してさらにEclipse起動する感じが面倒くさい 75 | -> 多分環境変数を切り出してantスクリプト実行出来れば良いのだろうけど、 76 | JetBrainsさんに頑張ってもらえないと辛い 77 | * コード補完は出来る 78 | * Eclipseの補助的な機能は死んでたりする 79 | 80 | 81 | 82 | 例えば文字列リテラルで 83 | String a = "a 84 | という状態で開業すると 85 | String a = "a" 86 | + " 87 | という風にEclipseはしてくれるはずなんだけどKotlin-eclipseだと出来ないとか。 88 | 89 | 90 | 91 | 92 | ### 参考URL 93 | 94 | [動画-Kotlin Eclipse Plugin @clomie #jkug](https://www.youtube.com/watch?v=6AZ6NjWXjzc) 95 | [スライド-Kotlin Eclipse Plugin #jkug by clomie](https://speakerdeck.com/clomie/kotlin-eclipse-plugin-number-jkug) 96 | -------------------------------------------------------------------------------- /docs/kotlin-plugin-gradle.md: -------------------------------------------------------------------------------- 1 | ### GradleのKotlinプラグインを使う 2 | -------------------------------------------------------------------------------- /docs/kotlin-plugin-intellij.md: -------------------------------------------------------------------------------- 1 | ## KotlinプラグインをIntelliJ IDEAに導入する 2 | 3 | ### 事前準備 4 | * IntelliJ IDEAのインストール 5 | 6 | ### プラグイン導入手順 7 | 8 | * IntelliJ IDEA起動 9 | * Preferencesを開く(Ctrl(cmd) + カンマもしくはIntelliJ IDEA>Preferences) 10 | * Pluginsを選択する 11 | ![図1](http://3.bp.blogspot.com/-IeqtRGH9heA/VP7ejZ5dpPI/AAAAAAAACRE/gCpTiY6K4M0/s1600/%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%2B2015-03-10%2B21.06.52.png) 12 | * Install JetBrains Plugin...を選択する 13 | ![図2](http://4.bp.blogspot.com/-VsJlgrXtGn0/VP7gGEG8Y2I/AAAAAAAACRU/I5LQ29tsayI/s1600/%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%2B2015-03-10%2B21.13.36.png) 14 | * Installを押す 15 | * IntelliJ IDEAを再起動 16 | 17 | 18 | 19 | 20 | これでプラグイン導入は完了です 21 | 22 | 23 | ### KotlinでHello Worldしてみよう 24 | * IntelliJ IDEA起動 25 | * New Projectを選択 26 | * 任意のプロジェクトを作成 27 | * プロジェクトを生成 28 | * File>New>Kotlin Fileを選択 29 | 30 | ### Hello World 31 | 32 | こんなかんじで書いてみましょう。 33 | 34 | fun main(args : Array) { 35 | println("hello Kotlin!") 36 | } 37 | 38 | 39 | * Run > Run Default Packageでプログラムを実行。 40 | 41 | hello Kotlin!と出力されたら成功です。 42 | 43 | ![図3](http://3.bp.blogspot.com/-Q6wHd05l_RE/VP7pZciMllI/AAAAAAAACRs/v1rI6pKTOd4/s1600/%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%2B2015-03-10%2B21.52.59.png) 44 | 45 | -------------------------------------------------------------------------------- /docs/kotlin-plugin-netbeans.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # KotlinをNetBeansで使う 4 | 5 | * インストール 6 | NetBeans 8.1でベータversionのKotlinプラグインが入手出来ます。 7 | 8 | ## Installation process: 9 | 10 | * NetBeansを起動します 11 | * Tools>Pluginsをmenuから選択 12 | * Downloadedタブを選択 13 | * Add Plugins…ボタンをクリック 14 | * エクスプローラでダウンロードされたファイルを選択 15 | * Pluginsダイアログでインストールボタンを押す 16 | * Nextを押しライセンスに同意する 17 | -------------------------------------------------------------------------------- /docs/loop.md: -------------------------------------------------------------------------------- 1 | ## ループ処理を行う 2 | 3 | ループ処理にはいくつかの構文があります。 4 | 5 | * for 6 | * do-while 7 | * while 8 | 9 | ### for 10 | 11 | 12 | フォーマットは以下のとおりです。 13 | 14 | 15 | for(任意の変数名 in イテレータブルなオブジェクト) { 16 | 処理 17 | } 18 | 19 | 20 | 21 | 実際には以下のように書きます。 22 | 23 | val arr = listOf(1, 2, 3, 4, 5) 24 | for (value in arr) { 25 | print(value) // 1 2 3 4 5 26 | } 27 | 28 | 29 | ### イテレータブルなオブジェクト 30 | 31 | 32 | イテレータブルというと難しそうですが、配列などをイメージしてもらえれば大体あっています。 33 | 以下のような性質を持つものの事を言います。 34 | 35 | * 関数 iterator()を持っている(クラスがjava.lang.Iterableを実装している必要はない) 36 | * 関数 next()を持っている、かつ、Boolean型の関数 next() またはプロパティnext を持っている 37 | 38 | 39 | ### for(int i = 0; i < 10; i++)とかをするには 40 | 41 | 42 | Javaのように条件式を書くことはできませんが、 43 | Range構文を使用して値の取りうる範囲と、ステップを指定する事ができます。 44 | 45 | 以下のように書きます。 46 | 47 | for (i in 0..4) print(i) // "01234" 48 | 49 | for (i in 0..4 step 2) print(i) // "024" 50 | 51 | for (i in 4 downTo 0) print(i) // "43210" 52 | 53 | for (i in 4 downTo 0 step 2) print(i) // "420" 54 | 55 | for (x in 1.0..2.0) print("$x ") // "1.0 2.0 " 56 | 57 | for (x in 1.0..2.0 step 0.3) print("$x ") // "1.0 1.3 1.6 1.9 " 58 | 59 | for (x in 2.0 downTo 1.0 step 0.3) print("$x ") // "2.0 1.7 1.4 1.1 " 60 | 61 | 62 | ### 参考 63 | 64 | [プログラミング言語Kotlin-3.4 制御構文](https://sites.google.com/site/tarokotlin/3-kotlinno-biao-zhunapi/sec34) 65 | 66 | [Kotlin Reference - Ranges](http://kotlinlang.org/docs/reference/ranges.html) 67 | 68 | 69 | 70 | whileとdo-whileに関しては他の言語とほぼ同じです。 71 | 72 | 73 | ### while 74 | 75 | 76 | フォーマットは以下のとおりです。 77 | 78 | 79 | 80 | while(条件){処理} 81 | 82 | 83 | 84 | 実装サンプルは以下のとおりです。 85 | 86 | 87 | var i = 0 88 | while (i < 10) { 89 | print(i++) // 0 1 2 3 4 5 6 7 8 9 90 | } 91 | ### do-while 92 | 93 | 94 | 95 | 96 | フォーマットは以下のとおりです。 97 | 98 | 99 | do{処理}while(条件) 100 | 101 | 102 | 実装サンプルは以下のとおりです。 103 | 104 | 105 | 106 | 107 | var i = 0 108 | do { 109 | print(i++) // 0 1 2 3 4 5 6 7 8 9 110 | } while(i < 10) 111 | 112 | 113 | 114 | 115 | 116 | 117 | -------------------------------------------------------------------------------- /docs/orm-for-kotlin.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # KotlinでSQLライブラリやORM(O/R Mapper)を使いたい 4 | 5 | 有力なものは以下のものがあります 6 | 7 | 8 | ## Exposed 9 | [https://github.com/JetBrains/Exposed](https://github.com/JetBrains/Exposed) 10 | 11 | [Exposedの使い方 - Javaビーム研究所 - ](http://blog.orekyuu.net/?p=484) 12 | 13 | ## kwery 14 | [https://github.com/andrewoma/kwery](https://github.com/andrewoma/kwery) 15 | 16 | ## Doma2 17 | [https://github.com/domaframework](https://github.com/domaframework) 18 | 19 | * サンプル 20 | [https://github.com/domaframework/kotlin-sample](https://github.com/domaframework/kotlin-sample) 21 | 22 | * Kotlinサポート - Doma 23 | [http://doma.readthedocs.io/ja/stable/kotlin-support](http://doma.readthedocs.io/ja/stable/kotlin-support) 24 | 25 | 26 | ## kotliquery 27 | 28 | [seratchさん](https://github.com/seratch)作 29 | [https://github.com/seratch/kotliquery](https://github.com/seratch/kotliquery) 30 | 31 | 32 | ## kotlin-nosql 33 | 34 | [https://github.com/cheptsov/kotlin-nosql](https://github.com/cheptsov/kotlin-nosql) 35 | 36 | 37 | -------------------------------------------------------------------------------- /docs/reflection.md: -------------------------------------------------------------------------------- 1 | # クラスのメタデータを取得する 2 | 3 | リフレクションによって、クラスのメタデータを取得する事ができます。 4 | 現状、Kotlin独自のメタデータ(プロパティ等)はKClass、 5 | Javaと共通のメタデータ(関数、コンストラクタ等)はJavaのClassを使用してアクセスします。 6 | (最新SnapShot(2015/8/11時点)では、KClassの操作のみでクラスの全情報にアクセスできるようになりつつあります) 7 | 8 | リフレクションによってクラスのメタデータを取得する方法は2種類あります。 9 | 10 | ### 静的に取得する 11 | 12 | クラスの静的情報から取得する場合、以下のように書きます。 13 | 14 | import kotlin.reflect.jvm.java 15 | 16 | class MyClass(){ 17 | val value = "MyClassのバリュー" 18 | fun add(x : String, y : String) = x + y 19 | } 20 | 21 | val kClass = MyClass::class // Kotlinのクラスインスタンス 22 | val jClass = kClass.java // Javaのクラスインスタンス 23 | 24 | 25 | ### 動的に取得する 26 | 27 | 動的に生成されるインスタンスから取得する場合、以下のように書きます。 28 | 29 | import kotlin.reflect.jvm.kotlin 30 | 31 | val obj = MyClass() // インスタンス生成 32 | val jClass = obj.javaClass // Javaのクラスインスタンス 33 | val kClass = jClass.kotlin // Kotlinのクラスインスタンス 34 | 35 | 36 | ## プロパティの取得 37 | 38 | KClassのメタデータを使用して、対象のオブジェクトのプロパティリストを取得するには、次のように書きます。 39 | 40 | import kotlin.reflect.jvm.javaGetter 41 | 42 | val obj = MyClass() 43 | val kClass = MyClass::class 44 | val properties = kClass.properties 45 | for(property in properties) { 46 | println(property.name + ":" + property.javaGetter?.invoke(obj)) // => value:MyClassのバリュー 47 | // こうも書ける println("${property.name}:${property.javaGetter?.invoke(obj)}") 48 | } 49 | 50 | この時のpropertiesの型は、Collection>となっており、 51 | 値を取得するだけでなく、設定も行いたい場合はKMutableMemberPropertyにキャストする必要があります。 52 | 53 | import kotlin.reflect.KMutableMemberProperty 54 | import kotlin.reflect.jvm.javaGetter 55 | import kotlin.reflect.jvm.javaSetter 56 | 57 | val obj = MyClass() 58 | val kClass = MyClass::class 59 | val properties = kClass.properties 60 | 61 | for(property in properties) { 62 | // 変更可能なメンバのみ、Setterが取得できる 63 | val setter = if (property is KMutableMemberProperty){property.javaSetter} else { null } 64 | // String型の場合にのみセットする 65 | if (setter?.getParameterTypes()?.get(0)?.isAssignableFrom(String.javaClass) ?: false){ 66 | setter?.invoke(obj, "set") 67 | } 68 | } 69 | 70 | そのクラスおよびスーパークラスで定義したプロパティは上記のpropertiesで、 71 | そのクラスおよびスーパークラスに対して定義された拡張プロパティはextensionProperties、 72 | それらのうちスーパークラスに定義されたものを除いたものはそれぞれdeclaredProperties、declaredExtensionPropertiesで 73 | 取得することができます。 74 | 75 | ##メソッドの呼び出し 76 | 77 | 現状、メソッドやコンストラクタを呼ぶにはJava側の機能を使用する必要があります。 78 | 79 | 80 | import kotlin.reflect.jvm.java 81 | 82 | val obj = MyClass() 83 | val jClass = obj.javaClass 84 | val method = jClass.getMethod("add", String::class.java, String::class.java) 85 | val result = method.invoke(obj, "X", "Y") 86 | println(result) // => XY 87 | 88 | Javaでinvokeメソッドを呼ぶ場合のように、コレクションにパラメータを作りこむ場合は、 89 | コレクションをスプレッド演算子(*)を使用して展開する。 90 | 91 | import kotlin.reflect.jvm.java 92 | 93 | val obj = MyClass() 94 | val jClass = obj.javaClass 95 | val method = jClass.getMethod("add", String::class.java, String::class.java) 96 | val params = arrayOf("XX", "YY") 97 | val result = method.invoke(obj, *params) 98 | println(result) // => XXYY 99 | 100 | -------------------------------------------------------------------------------- /docs/todo.md: -------------------------------------------------------------------------------- 1 | 2 | # TODOタスクをソースに残す 3 | 4 | 5 | Kotlinの場合TODO関数が用意されており、実行時にエラーにしてくれたります。 6 | もちろんJavaと同様にコメントとしてTODOを残すことも可能です。 7 | 8 | 9 | 関数の定義 10 | 11 | inline fun TODO(): Nothing (source) 12 | inline fun TODO(reason: String): Nothing (source) 13 | 14 | 使い方 15 | 16 | TODO() 17 | TODO("hogehogeなためfugafugaする") 18 | 19 | 20 | こうすると実行時に例外が投げられます。 21 | 22 | Exception in thread "main" kotlin.NotImplementedError: An operation is not implemented: hogehogeなためfugafugaする 23 | at sample.Simplest_versionKt.main(Simplest version.kt:11) 24 | 25 | [https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-t-o-d-o.html](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-t-o-d-o.html) 26 | -------------------------------------------------------------------------------- /docs/try-with-resources-for-kotlin.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # Kotlinでtry-with-resourcesが使いたい 4 | 5 | 6 | 7 | useを使います。 8 | 9 | val inputStream = Thread.currentThread().contextClassLoader.getResourceAsStream("hogehoge") 10 | java.io.InputStreamReader(inputStream).use { 11 | // `it`がInputStreamReaderのインスタンス 12 | val b = it.read() 13 | } 14 | 15 | 16 | useはClosableインターフェースを実装しているクラスの拡張関数として提供されています。 17 | したがってcloseが実装されているクラスには同じように適用できます。 18 | 19 | [https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/use.html](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/use.html) 20 | -------------------------------------------------------------------------------- /docs/type-alias.md: -------------------------------------------------------------------------------- 1 | 2 | # type aliasについて 3 | 4 | Kotlinのtype aliasはその名の通り、型に対して別名(エイリアス)を付ける機能です。 5 | 6 | 7 | ## version 8 | 9 | Kotlin 1.1以降 10 | 11 | ## 文法 12 | 13 |    typealias 任意の型名 = 対応する型 14 |     15 | ## 書き方 16 | 17 | * Function Type 18 | 19 | ```kotlin 20 | typealias MyHandler = (Int, String, Any) -> Unit 21 | typealias HtmlBuilderAction = HtmlBuilder.() -> Unit 22 | typealias Predicate = (T) -> Boolean 23 | ``` 24 | 25 | * コレクション 26 |     27 | ```kotlin 28 |    typealias NodeSet = Set 29 | typealias FilesTable = MutableMap> 30 | ``` 31 | 32 | * Nestしたクラス 33 | 34 | ```kotlin 35 | class Outer { 36 | class Nested { 37 | inner class Inner 38 | } 39 | } 40 | typealias Something = Outer.Nested.Inner 41 | ``` 42 |     43 | ## 参考URL 44 | 45 | https://github.com/Kotlin/KEEP/blob/master/proposals/type-aliases.md 46 | -------------------------------------------------------------------------------- /docs/type-safe-builder.md: -------------------------------------------------------------------------------- 1 | 2 | # 親子関係のある独自のブロックを作りたい 3 | 4 | Groovy風のType-Safe buildersを作りましょう。 5 | 6 | 7 | [Type-Safe Builders](https://kotlinlang.org/docs/reference/type-safe-builders.html) 8 | 9 | 10 | ### 関数を引数に渡すと出来そう 11 | 12 | 単純にブロックだけを作ろうとするとこういう関数を定義することになります。 13 | 14 | 15 | fun parent(f: () -> Unit){} 16 | fun child (f:() -> Unit){} 17 | 18 | 19 | 20 | 21 | ### 問題点 22 | 23 | この関数には親子関係がないので、こう書けてしまいます。 24 | 25 | 26 | parent{ 27 | println("おや") 28 | } 29 | 30 | child { 31 | println("こども") 32 | } 33 | 34 | 35 | ### 親子関係をType-Safe Buildersで表現 36 | 37 | これも防ぎたい、あとうまく親子関係で情報を受け渡したい場合に 38 | Type-Safe Buildersです。 39 | 40 | 41 | fun parent(init: Parent.() -> Unit) : Parent{ 42 | val parent = Parent() 43 | parent.init() 44 | return parent 45 | } 46 | 47 | class Parent(){ 48 | fun child(init: Child.() -> Unit) : Child { 49 | val child = Child() 50 | child.init() 51 | return child 52 | } 53 | } 54 | 55 | class Child 56 | 57 | 58 | ## 使い方 59 | 60 | 61 | 62 | さっきのがコンパイルエラーになります 63 | 64 | 65 | parent{ 66 | println("おや") 67 | } 68 | 69 | child { // コンパイルエラー!! 70 | println("こども") 71 | } 72 | 73 | 74 | ## 解説 75 | 76 | 77 | Type-Safe Buildersのポイントはレシーバーです。 78 | 79 | 「クラス名().->」と書くことで、当該クラスをレシーバーとしてそのクラスインスタンスに従属する引数なしの関数という定義になります。 80 | 81 | これを組み合わせると前述のようにクラスでうまく隠蔽や情報の伝達が可能になります。 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /docs/type.md: -------------------------------------------------------------------------------- 1 | ## 型を宣言する 2 | 型の宣言は変数の後に:(コロン) + 形名で表現します。 3 | 関数の戻り値の型指定に関しても同様です。 4 | 5 | 6 | 7 | ### 型の宣言(変数) 8 | 9 | val hoge : String = "a" 10 | val fuga : Int = 1 11 | val piyo : Double = 1.0 12 | 13 | 型は基本的に大文字始まりです。 14 | Javaではintやdoubleなどプリミティブものは小文字始まりですが、 15 | Kotlinでは全てをクラスとして扱います。 16 | 17 | 18 | 19 | 20 | ### 型の宣言(関数) 21 | 22 | fun createHogeString() : String = "Hoge" 23 | 24 | 25 | 関数の型宣言も同様に:(コロン) + 型名で表現します。 26 | 27 | 28 | 29 | 30 | ### null許容型の宣言 31 | 32 | 33 | [変数の宣言の項](../variable)でも説明しましたが、 34 | Kotlinでは基本的にnull(何も値が無い状態)を許容しません。 35 | 36 | nullを許容する場合には:(コロン) + 型名のあとに「?」をつけます。 37 | 38 | val hoge : String? = null // OK 39 | val hoge : String = null // コンパイルエラー 40 | 41 | 42 | fun nullValue() : String? = null // OK 43 | fun nullValue() : String = null // コンパイルエラー 44 | 45 | 基本的にはnull許さないよう言語設計されています。 46 | 47 | 48 | 49 | 50 |
51 |
52 |
53 |
54 | NULL 55 | >NULLはNULL値(あるいは空値)と呼ばれることもあるが、 56 | >コッドによると値ではない(特殊な値や例外的な値ではない)。 57 | >値ではないので型もない。 58 | >整数型の列にあるNULLも文字列型の列にあるNULLも同じNULLであり、 59 | >NULL整数・NULL文字列などの区別はない。 60 | >内部的にはNULLは、(NaNやヌルポインタのような)特定のビット列ではなく、 61 | >値とは別個の「NULLかどうか」を表すメモリ領域で管理されていることが多い。 62 | 63 | (Wikipediaより) 64 | 65 | 66 | -------------------------------------------------------------------------------- /docs/unit-test-for-kotlin.md: -------------------------------------------------------------------------------- 1 | 2 | # Kotlinでユニットテストを書く 3 | 4 | KotlinでテスティングフレームワークはJUnitかspekなどがあります。 5 | spekは2017年1月9日現在、バージョンは1.1.0-beta2です。(いつの間にメジャーバージョン出たのか)。JUnit5対応もしている。 6 | 7 | ## JUnit 8 | 9 | JUnitは普通に依存関係に入れてください。 10 | 11 | ### Maven 12 | 13 | 14 | junit 15 | junit 16 | 4.12 17 | 18 | 19 | 20 | ### Gradle 21 | 22 | testCompile 'junit:junit:4.12' 23 | 24 | 25 | ### コード 26 | 27 | Javaとさほど変わりません。がhamcrest.CorMatchers.isがKotlinの予約語とかぶるので、バッククオート「`」で囲ってあげないといけません。 28 | 29 | 30 | import kebab.Browser 31 | import org.junit.Test 32 | import org.junit.Assert.assertThat 33 | import org.hamcrest.CoreMatchers.`is` 34 | /** 35 | * Created by yy_yank on 2016/02/27. 36 | */ 37 | class BrowserTest { 38 | 39 | val sut : Browser = Browser() 40 | 41 | @Test fun testCalculateURL() { 42 | val map = mapOf(Pair("1",1), Pair("2",2) , Pair("3", 3)) 43 | val actual = sut.calculateUri("abcd", map) 44 | assertThat(actual, `is`("")) 45 | } 46 | } 47 | 48 | 49 | assertEqualsとかは普通に何も意識せずに使えます。 50 | はまりどころはこれぐらい。 51 | AssertJなども普通に使えます。 52 | 53 | 54 | ## spek 55 | 56 | 57 | spekはJetBrains製のKotlinのテスティングフレームワーク。 58 | 59 | [http://spekframework.org/docs/latest/](http://spekframework.org/docs/latest/) 60 | 61 | 62 | Spekクラスを継承してgiven-on-itのスタイルでテストが出来ます。 63 | 64 | ### Maven 65 | 66 | 67 | org.jetbrains.spek 68 | spek-api 69 | 1.1.0-beta2 70 | 71 | 72 | 73 | 74 | ### Gradle 75 | 76 | build.gradleを以下のように書く 77 | 78 | // setup the plugin 79 | buildscript { 80 | dependencies { 81 | classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M3' 82 | } 83 | } 84 | 85 | apply plugin: 'org.junit.platform.gradle.plugin' 86 | 87 | junitPlatform { 88 | filters { 89 | engines { 90 | include 'spek' 91 | } 92 | } 93 | } 94 | 95 | repositories { 96 | maven { url "http://dl.bintray.com/jetbrains/spek" } 97 | } 98 | 99 | // setup dependencies 100 | dependencies { 101 | testCompile 'org.jetbrains.spek:spek-api:1.1.0-beta3' 102 | testRuntime 'org.jetbrains.spek:spek-junit-platform-engine:1.1.0-beta3' 103 | } 104 | 105 | 106 | 107 | 108 | build.ktsの場合はこのようにかく 109 | 110 | 111 | import org.gradle.api.plugins.ExtensionAware 112 | 113 | import org.junit.platform.gradle.plugin.FiltersExtension 114 | import org.junit.platform.gradle.plugin.EnginesExtension 115 | import org.junit.platform.gradle.plugin.JUnitPlatformExtension 116 | 117 | // setup the plugin 118 | buildscript { 119 | dependencies { 120 | classpath("org.junit.platform:junit-platform-gradle-plugin:1.0.0-M3") 121 | } 122 | } 123 | 124 | apply { 125 | plugin("org.junit.platform.gradle.plugin") 126 | } 127 | 128 | configure { 129 | filters { 130 | engines { 131 | include("spek") 132 | } 133 | } 134 | } 135 | 136 | // setup dependencies 137 | dependencies { 138 | testCompile("org.jetbrains.spek:spek-api:1.1.0-beta3") 139 | testRuntime("org.jetbrains.spek:spek-junit-platform-engine:1.1.0-beta3") 140 | } 141 | 142 | // extension for configuration 143 | fun JUnitPlatformExtension.filters(setup: FiltersExtension.() -> Unit) { 144 | when (this) { 145 | is ExtensionAware -> extensions.getByType(FiltersExtension::class.java).setup() 146 | else -> throw Exception("${this::class} must be an instance of ExtensionAware") 147 | } 148 | } 149 | fun FiltersExtension.engines(setup: EnginesExtension.() -> Unit) { 150 | when (this) { 151 | is ExtensionAware -> extensions.getByType(EnginesExtension::class.java).setup() 152 | else -> throw Exception("${this::class} must be an instance of ExtensionAware") 153 | } 154 | } 155 | 156 | 157 | 158 | 159 | ### コード 160 | 161 | 公式ドキュメントより。 162 | 163 | object SimpleSpec: Spek({ 164 | describe("a calculator") { 165 | val calculator = SampleCalculator() 166 | on("addition") { 167 | val sum = calculator.sum(2, 4) 168 | it("should return the result of adding the first number to the second number") { 169 | assertEquals(6, sum) 170 | } 171 | } 172 | 173 | on("subtraction") { 174 | val subtract = calculator.subtract(4, 2) 175 | 176 | it("should return the result of subtracting the second number from the first number") { 177 | assertEquals(2, subtract) 178 | } 179 | } 180 | } 181 | }) 182 | 183 | 184 | 185 | ## KotlinTest 186 | 187 | これもspekと同じくスペックテスト的なものをするためのライブラリ。ScalaTestに影響を受けている。 188 | [https://github.com/kotlintest/kotlintest](https://github.com/kotlintest/kotlintest) 189 | 190 | ### Maven 191 | 192 | 193 | io.kotlintest 194 | kotlintest 195 | xxx 196 | test 197 | 198 | 199 | 200 | ### Gradle 201 | 202 | testCompile 'io.kotlintest:kotlintest:xxx' 203 | 204 | 205 | ### コード 206 | 207 | 208 | 公式ドキュメントより。 209 | 210 | class StringSpecExample : StringSpec() { 211 | init { 212 | "strings.length should return size of string" { 213 | "hello".length shouldBe 5 214 | } 215 | } 216 | } 217 | -------------------------------------------------------------------------------- /docs/variable.md: -------------------------------------------------------------------------------- 1 | ## 変数を宣言する 2 | 変数の宣言には2種類の宣言があります 3 | 4 | * var 5 | * val 6 | 7 | varで宣言した場合は変数への再代入可能です。 8 | valで宣言した場合は変数への再代入はできません。 9 | 10 | 11 | 12 | var hoge = "a" 13 | hoge = "b" //OK 14 | val fuga = "a" 15 | fuga = "b" // コンパイルエラー 16 | 17 | varはいつでもどこでも代入できてしまいます。 18 | valで宣言すると再代入時にコンパイルエラーにしてくれます。 19 | 代入を何度も繰り返す破壊的代入は良くないので、 20 | 基本的にはvalを使いましょう 21 | 22 | ※varを使わざるを得ないケースもあります 23 | 24 | 25 | 26 | ### 文字列の宣言 27 | 28 | // 文字列の宣言 29 | val hoge = "aiueo" 30 | // 型の宣言も可能 31 | val hoge : String = "aiueo" 32 | // 数値の宣言 33 | val number = 1 34 | 35 | 型の宣言は明示的にすることも可能ですが、 36 | 推論されるので宣言しなくても良いです。 37 | 38 | 39 | 40 | ### nullは?を付けないと代入できない 41 | 42 | // コンパイルエラー! 43 | val a = null 44 | // これならnullを代入してもOK 45 | val a : String? = null 46 | 47 | こういうこと言われてしまいます 48 | 「 is not satisfied: inferred type kotlin.Nothing? is not a subtype of kotlin.Any」 49 | null使わないほうがいいですね 50 | 51 | 52 | どうしてもnullを許容するためには型を宣言して「?」を付ける必要があります。 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 |
63 |
64 |
65 |
66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /docs/when.md: -------------------------------------------------------------------------------- 1 | ## switchみたいな条件分岐がしたい 2 | 3 | ### when 4 | 5 | Kotlinにはswitch構文はありません。 6 | 代替としてwhenを使います。 7 | ifと同様にwhenも式(when式)です。 8 | 9 | ### フォーマット 10 | 11 | 12 | 以下のように記述します。 13 | 14 | ``` 15 | when(引数) { 16 | 値-> 戻り値 17 | else -> 戻り値 18 | } 19 | 20 | when { 21 |        条件-> 戻り値 22 | else -> 戻り値 23 | } 24 | 25 | ``` 26 | 27 | 28 | elseは必須となっております 29 | 30 | 31 | ### 実装サンプル 32 | 33 | 34 | 35 | 36 | val value = "hoge" 37 | when { 38 | value == "hoge" -> println("ほげ") 39 | value == "fuga" -> println("ふが") 40 | else -> println("piyo") 41 | } 42 | 43 | 44 | よくあるswitch的な書き方だとこんな感じです。 45 | 46 | val value = "hoge" 47 | when(value) { 48 | "hoge" -> println("ほげ") 49 | "fuga" -> println("ふが") 50 | else -> println("piyo") 51 | } 52 | 53 | 54 | 55 | 56 | 57 | Stringの戻り値を持つwhenはこのように書きます。 58 | 59 | 60 | val value = "hoge" 61 | val result = when(value) { 62 | "hoge" -> "ほげ" 63 | "fuga" -> "ふが" 64 | else -> "piyo" 65 | } 66 | println(result) // => ほげ 67 | 68 | 69 | -------------------------------------------------------------------------------- /images/Kotlin-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yyYank/kotlin-rev-solution/6369e30949e503abac9627ff21c5b1688b4e4fdd/images/Kotlin-icon.png -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 逆引きKotlin 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /mkdocs.yml: -------------------------------------------------------------------------------- 1 | site_name: 逆引きKotlin 2 | site_url: http://kotlin-rev-solution.herokuapp.com/site 3 | repo_url: https://github.com/yyYank/kotlin-rev-solution 4 | site_description: JVM言語、Kotlin(ことりん)の逆引きサイトです。基本的な環境導入、文法から、あんなことがしたいこんなことがしたいというKotlinでやりたいことを網羅的に逆引き出来るサイトを目指しています。 5 | site_author: yy_yank 6 | theme_dir: 'themes' 7 | pages: 8 | - [index.md, Home] 9 | - [kotlin-install.md, '導入', 'Kotlinをインストールする'] 10 | - [kotlin-plugin-intellij.md, '導入', 'KotlinプラグインをIntelliJ IDEAに導入する'] 11 | - [kotlin-plugin-androidstudio.md, '導入', 'KotlinプラグインをAndroid Studioに入れる'] 12 | - [kotlin-plugin-eclipse.md, '導入', 'KotlinをEclipseで動かす'] 13 | - [hello-kotlin-android.md, '導入', 'AndroidでHelloWorld'] 14 | - [variable.md, '基本文法', '変数を宣言する'] 15 | - [type.md, '基本文法', '型を宣言する'] 16 | - [instance.md, '基本文法', 'インスタンスを生成する'] 17 | - [array.md, '基本文法', '配列を生成する'] 18 | - [function.md, '基本文法', '関数を宣言する'] 19 | - [function_type.md, '基本文法', '好きなタイミングで特定の処理を行う'] 20 | - [function_object.md, '基本文法', '関数をオブジェクトとして使う'] 21 | - [if-statement.md, '基本文法', '条件分岐を行う'] 22 | - [if-function.md, '基本文法', 'ifを式として扱う'] 23 | - [when.md, '基本文法', 'switchみたいな条件分岐がしたい'] 24 | - [loop.md, '基本文法', 'ループ処理を行う'] 25 | - [enum.md, '基本文法', 'enumを使いたい'] 26 | - [kotlin-collection-framework.md, 'コレクション操作', 'Kotlinのコレクション・フレームワークを知りたい'] 27 | - [collection-for-each.md, 'コレクション操作', 'リストの要素分ループして操作する'] 28 | - [collection-filter.md, 'コレクション操作', '条件でリストを絞り込む'] 29 | - [collection-map.md, 'コレクション操作', 'リストの要素の名前のみを抽出する'] 30 | - [collection-merge.md, 'コレクション操作', 'リストをマージする'] 31 | - [collection-sum.md, 'コレクション操作', 'リストの値を集計する'] 32 | - [collection-drop.md, 'コレクション操作', 'リストの要素を前からいくつか削る'] 33 | - [collection-take.md, 'コレクション操作', 'リストの要素を前からいくつか取り出す'] 34 | - [collection-zip.md, 'コレクション操作', '2つのリストのペアのリストを作る'] 35 | - [extension.md, '応用編', 'クラスの機能拡張をしたい'] 36 | - [type-safe-builder.md, '応用編', '親子関係のある独自のブロックを作りたい'] 37 | - [reflection.md, '応用編', 'クラスのメタデータを操作する'] 38 | - [framework-for-kotlin.md, '応用編', 'Kotlinでフレームワークを使いたい'] 39 | - [unit-test-for-kotlin.md, '応用編', 'ユニットテストをしたい'] 40 | - [keyword-for-kotlin.md, '応用編', 'Kotlinのキーワードを知りたい'] 41 | - [orm-for-kotlin.md, '応用編', 'KotlinでORM(O/R Mapper)を使いたい'] 42 | - [try-with-resources-for-kotlin.md, '応用編', 'Kotlinでtry-with-resourcesが使いたい'] 43 | - [todo.md, '応用編', 'TODOタスクをソースに残す'] 44 | - [kclass-argument.md, '応用編', 'クラスを引数にとる関数をつくる'] 45 | - [gradle-kts.md, '応用編', 'GradleのビルドスクリプトをKotlinで書く'] 46 | - [type-alias.md, 'ver1.1', 'よく使う型に別名をつけたい'] 47 | - [coroutines-for-kotlin.md, 'ver1.1', 'コールバックヘルとサヨナラしたい'] 48 | - [all-open-plugin.md, 'ver1.1', 'SpringのAOPやMockitoでopenって書きたくない'] 49 | 50 | -------------------------------------------------------------------------------- /site/404.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 逆引きKotlin 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 381 | 382 |
383 | 384 | 385 |
386 |
387 |

404

388 |

Page not found

389 |
390 |
391 | 392 | 393 |
394 | 395 |
396 |
397 | 398 |

Documentation built with MkDocs.

399 |
400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 432 | 433 | 434 | 435 | -------------------------------------------------------------------------------- /site/base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | {% if page_description %}{% endif %} 8 | {% if site_author %}{% endif %} 9 | {% if canonical_url %}{% endif %} 10 | {% if favicon %} 11 | {% else %}{% endif %} 12 | 13 | {% if page_title %}{{ page_title }} - {% endif %}{{ site_name }} 14 | 15 | 16 | 17 | 18 | 19 | {%- for path in extra_css %} 20 | 21 | {%- endfor %} 22 | 23 | 24 | 28 | 29 | {% if google_analytics %} 30 | 39 | {% endif %} 40 | 41 | 42 | 43 | 44 | {% include "nav.html" %} 45 | 46 |
47 | {% block content %} 48 |
{% include "toc.html" %}
49 |
{% include "content.html" %}
50 | {% endblock %} 51 |
52 | 53 |
54 |
55 | {% if copyright %} 56 |

{{ copyright }}

57 | {% endif %} 58 |

Documentation built with MkDocs.

59 |
60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | {%- for path in extra_javascript %} 68 | 69 | {%- endfor %} 70 | 71 | 95 | 96 | 97 | 98 | {% if current_page and current_page.is_homepage %} 99 | 103 | {% endif %} 104 | -------------------------------------------------------------------------------- /site/content.html: -------------------------------------------------------------------------------- 1 | {% if meta.source %} 2 | 7 | {% endif %} 8 | 9 | {{ content }} 10 | -------------------------------------------------------------------------------- /site/css/base.css: -------------------------------------------------------------------------------- 1 | body { 2 | padding-top: 70px; 3 | background: url(../img/grid.png) repeat-x; 4 | background-attachment: fixed; 5 | background-color: #f8f8f8; 6 | } 7 | 8 | body > div.container { 9 | min-height: 400px; 10 | } 11 | 12 | ul.nav li.main { 13 | font-weight: bold; 14 | } 15 | 16 | div.col-md-3 { 17 | padding-left: 0; 18 | } 19 | 20 | div.col-md-9 { 21 | padding-bottom: 100px; 22 | } 23 | 24 | div.source-links { 25 | float: right; 26 | } 27 | 28 | div.col-md-9 img { 29 | max-width: 100%; 30 | display: inline; 31 | padding: 4px; 32 | line-height: 1.428571429; 33 | background-color: #fff; 34 | border: 1px solid #ddd; 35 | border-radius: 4px; 36 | margin: 20px auto 30px auto; 37 | } 38 | 39 | h1, h2, h3 { 40 | color: #444; 41 | } 42 | 43 | h1[id]:before, h2[id]:before, h3[id]:before, h4[id]:before, h5[id]:before, h6[id]:before { 44 | content: ""; 45 | display: block; 46 | margin-top: -75px; 47 | height: 75px; 48 | pointer-events: none; 49 | } 50 | 51 | h1 { 52 | font-weight: 400; 53 | font-size: 42px; 54 | } 55 | 56 | h2, h3, h4, h5, h6 { 57 | font-weight: 300; 58 | } 59 | 60 | hr { 61 | border-top: 1px solid #aaa; 62 | } 63 | 64 | pre, .rst-content tt { 65 | max-width: 100%; 66 | background: #fff; 67 | border: solid 1px #e1e4e5; 68 | color: #e74c3c; 69 | overflow-x: auto; 70 | } 71 | 72 | code.code-large, .rst-content tt.code-large { 73 | font-size: 90%; 74 | } 75 | 76 | code { 77 | padding: 2px 5px; 78 | color: #c7254e; 79 | background-color: #f9f2f4; 80 | font-size: 75%; 81 | } 82 | 83 | code, kbd, pre, samp { 84 | font-family: monospace,serif; 85 | font-size: 12px !important; 86 | } 87 | 88 | footer { 89 | margin-top: 30px; 90 | margin-bottom: 10px; 91 | text-align: center; 92 | font-weight: 200; 93 | } 94 | 95 | .modal-dialog { 96 | margin-top: 60px; 97 | } 98 | 99 | /* 100 | * Side navigation 101 | * 102 | * Scrollspy and affixed enhanced navigation to highlight sections and secondary 103 | * sections of docs content. 104 | */ 105 | 106 | /* By default it's not affixed in mobile views, so undo that */ 107 | .bs-sidebar.affix { 108 | position: static; 109 | } 110 | 111 | .bs-sidebar.well { 112 | padding: 0; 113 | max-height: 90%; 114 | overflow-y: auto; 115 | } 116 | 117 | /* First level of nav */ 118 | .bs-sidenav { 119 | padding-top: 10px; 120 | padding-bottom: 10px; 121 | border-radius: 5px; 122 | } 123 | 124 | /* All levels of nav */ 125 | .bs-sidebar .nav > li > a { 126 | display: block; 127 | padding: 5px 20px; 128 | z-index: 1; 129 | } 130 | .bs-sidebar .nav > li > a:hover, 131 | .bs-sidebar .nav > li > a:focus { 132 | text-decoration: none; 133 | border-right: 1px solid; 134 | } 135 | .bs-sidebar .nav > .active > a, 136 | .bs-sidebar .nav > .active:hover > a, 137 | .bs-sidebar .nav > .active:focus > a { 138 | font-weight: bold; 139 | background-color: transparent; 140 | border-right: 1px solid; 141 | } 142 | 143 | /* Nav: second level (shown on .active) */ 144 | .bs-sidebar .nav .nav { 145 | display: none; /* Hide by default, but at >768px, show it */ 146 | margin-bottom: 8px; 147 | } 148 | .bs-sidebar .nav .nav > li > a { 149 | padding-top: 3px; 150 | padding-bottom: 3px; 151 | padding-left: 30px; 152 | font-size: 90%; 153 | } 154 | 155 | /* Show and affix the side nav when space allows it */ 156 | @media (min-width: 992px) { 157 | .bs-sidebar .nav > .active > ul { 158 | display: block; 159 | } 160 | /* Widen the fixed sidebar */ 161 | .bs-sidebar.affix, 162 | .bs-sidebar.affix-bottom { 163 | width: 213px; 164 | } 165 | .bs-sidebar.affix { 166 | position: fixed; /* Undo the static from mobile first approach */ 167 | top: 80px; 168 | } 169 | .bs-sidebar.affix-bottom { 170 | position: absolute; /* Undo the static from mobile first approach */ 171 | } 172 | .bs-sidebar.affix-bottom .bs-sidenav, 173 | .bs-sidebar.affix .bs-sidenav { 174 | margin-top: 0; 175 | margin-bottom: 0; 176 | } 177 | } 178 | @media (min-width: 1200px) { 179 | /* Widen the fixed sidebar again */ 180 | .bs-sidebar.affix-bottom, 181 | .bs-sidebar.affix { 182 | width: 263px; 183 | } 184 | } 185 | 186 | .headerlink { 187 | font-family: FontAwesome; 188 | font-size: 14px; 189 | display: none; 190 | padding-left: .5em; 191 | } 192 | 193 | h1:hover .headerlink, h2:hover .headerlink, h3:hover .headerlink, h4:hover .headerlink, h5:hover .headerlink, h6:hover .headerlink{ 194 | display:inline-block; 195 | } 196 | 197 | 198 | 199 | .admonition { 200 | padding: 15px; 201 | margin-bottom: 20px; 202 | border: 1px solid transparent; 203 | border-radius: 4px; 204 | text-align: left; 205 | } 206 | 207 | .admonition.note { 208 | color: #3a87ad; 209 | background-color: #d9edf7; 210 | border-color: #bce8f1; 211 | } 212 | 213 | .admonition.warning { 214 | color: #c09853; 215 | background-color: #fcf8e3; 216 | border-color: #fbeed5; 217 | } 218 | 219 | .admonition.danger { 220 | color: #b94a48; 221 | background-color: #f2dede; 222 | border-color: #eed3d7; 223 | } 224 | 225 | .admonition-title { 226 | font-weight: bold; 227 | text-align: left; 228 | } 229 | 230 | 231 | .dropdown-submenu { 232 | position: relative; 233 | } 234 | 235 | .dropdown-submenu>.dropdown-menu { 236 | top: 0; 237 | left: 100%; 238 | margin-top: -6px; 239 | margin-left: -1px; 240 | -webkit-border-radius: 0 6px 6px 6px; 241 | -moz-border-radius: 0 6px 6px; 242 | border-radius: 0 6px 6px 6px; 243 | } 244 | 245 | .dropdown-submenu:hover>.dropdown-menu { 246 | display: block; 247 | } 248 | 249 | .dropdown-submenu>a:after { 250 | display: block; 251 | content: " "; 252 | float: right; 253 | width: 0; 254 | height: 0; 255 | border-color: transparent; 256 | border-style: solid; 257 | border-width: 5px 0 5px 5px; 258 | border-left-color: #ccc; 259 | margin-top: 5px; 260 | margin-right: -10px; 261 | } 262 | 263 | .dropdown-submenu:hover>a:after { 264 | border-left-color: #fff; 265 | } 266 | 267 | .dropdown-submenu.pull-left { 268 | float: none; 269 | } 270 | 271 | .dropdown-submenu.pull-left>.dropdown-menu { 272 | left: -100%; 273 | margin-left: 10px; 274 | -webkit-border-radius: 6px 0 6px 6px; 275 | -moz-border-radius: 6px 0 6px 6px; 276 | border-radius: 6px 0 6px 6px; 277 | } 278 | -------------------------------------------------------------------------------- /site/css/highlight.css: -------------------------------------------------------------------------------- 1 | /* 2 | This is the GitHub theme for highlight.js 3 | 4 | github.com style (c) Vasily Polovnyov 5 | 6 | */ 7 | 8 | .hljs { 9 | display: block; 10 | overflow-x: auto; 11 | padding: 0.5em; 12 | color: #333; 13 | -webkit-text-size-adjust: none; 14 | } 15 | 16 | .hljs-comment, 17 | .diff .hljs-header, 18 | .hljs-javadoc { 19 | color: #998; 20 | font-style: italic; 21 | } 22 | 23 | .hljs-keyword, 24 | .css .rule .hljs-keyword, 25 | .hljs-winutils, 26 | .nginx .hljs-title, 27 | .hljs-subst, 28 | .hljs-request, 29 | .hljs-status { 30 | color: #333; 31 | font-weight: bold; 32 | } 33 | 34 | .hljs-number, 35 | .hljs-hexcolor, 36 | .ruby .hljs-constant { 37 | color: #008080; 38 | } 39 | 40 | .hljs-string, 41 | .hljs-tag .hljs-value, 42 | .hljs-phpdoc, 43 | .hljs-dartdoc, 44 | .tex .hljs-formula { 45 | color: #d14; 46 | } 47 | 48 | .hljs-title, 49 | .hljs-id, 50 | .scss .hljs-preprocessor { 51 | color: #900; 52 | font-weight: bold; 53 | } 54 | 55 | .hljs-list .hljs-keyword, 56 | .hljs-subst { 57 | font-weight: normal; 58 | } 59 | 60 | .hljs-class .hljs-title, 61 | .hljs-type, 62 | .vhdl .hljs-literal, 63 | .tex .hljs-command { 64 | color: #458; 65 | font-weight: bold; 66 | } 67 | 68 | .hljs-tag, 69 | .hljs-tag .hljs-title, 70 | .hljs-rule .hljs-property, 71 | .django .hljs-tag .hljs-keyword { 72 | color: #000080; 73 | font-weight: normal; 74 | } 75 | 76 | .hljs-attribute, 77 | .hljs-variable, 78 | .lisp .hljs-body, 79 | .hljs-name { 80 | color: #008080; 81 | } 82 | 83 | .hljs-regexp { 84 | color: #009926; 85 | } 86 | 87 | .hljs-symbol, 88 | .ruby .hljs-symbol .hljs-string, 89 | .lisp .hljs-keyword, 90 | .clojure .hljs-keyword, 91 | .scheme .hljs-keyword, 92 | .tex .hljs-special, 93 | .hljs-prompt { 94 | color: #990073; 95 | } 96 | 97 | .hljs-built_in { 98 | color: #0086b3; 99 | } 100 | 101 | .hljs-preprocessor, 102 | .hljs-pragma, 103 | .hljs-pi, 104 | .hljs-doctype, 105 | .hljs-shebang, 106 | .hljs-cdata { 107 | color: #999; 108 | font-weight: bold; 109 | } 110 | 111 | .hljs-deletion { 112 | background: #fdd; 113 | } 114 | 115 | .hljs-addition { 116 | background: #dfd; 117 | } 118 | 119 | .diff .hljs-change { 120 | background: #0086b3; 121 | } 122 | 123 | .hljs-chunk { 124 | color: #aaa; 125 | } 126 | -------------------------------------------------------------------------------- /site/enum/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | enumを使いたい - 逆引きKotlin 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 381 | 382 |
383 | 384 |
392 |
393 | 394 |

enumを使いたい

395 |

Javaと同等のenumがあります。

396 |

Enum Classes

397 |
enum class Direction {
398 |   NORTH, SOUTH, WEST, EAST
399 | }
400 | 
401 | 402 |
403 | 404 |
405 |
406 | 407 |

Documentation built with MkDocs.

408 |
409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 441 | 442 | 443 | 444 | -------------------------------------------------------------------------------- /site/fonts/fontawesome-webfont.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yyYank/kotlin-rev-solution/6369e30949e503abac9627ff21c5b1688b4e4fdd/site/fonts/fontawesome-webfont.eot -------------------------------------------------------------------------------- /site/fonts/fontawesome-webfont.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yyYank/kotlin-rev-solution/6369e30949e503abac9627ff21c5b1688b4e4fdd/site/fonts/fontawesome-webfont.ttf -------------------------------------------------------------------------------- /site/fonts/fontawesome-webfont.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yyYank/kotlin-rev-solution/6369e30949e503abac9627ff21c5b1688b4e4fdd/site/fonts/fontawesome-webfont.woff -------------------------------------------------------------------------------- /site/img/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yyYank/kotlin-rev-solution/6369e30949e503abac9627ff21c5b1688b4e4fdd/site/img/favicon.ico -------------------------------------------------------------------------------- /site/img/grid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yyYank/kotlin-rev-solution/6369e30949e503abac9627ff21c5b1688b4e4fdd/site/img/grid.png -------------------------------------------------------------------------------- /site/instance/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | インスタンスを生成する - 逆引きKotlin 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 381 | 382 |
383 | 384 |
396 |
397 | 398 |

インスタンスを生成する

399 |

フォーマット

400 |

Kotlinにおいてクラスのインスタンスを生成するには

401 |
クラス名(コンストラクタ引数)
402 | 
403 |

の形式で指定します。new演算子は使いません。

404 |

実装サンプル

405 |

例えば、以下の様なHogeというクラスが有った時

406 |
public class Hoge(val message : String) {
407 |    fun printHoge () {
408 |        println("helllo! -> $message")
409 |    }
410 | }
411 | 
412 |

インスタンスの生成は以下のように行います。

413 |
val hoge = Hoge("hoge")
414 | 
415 | 416 |
417 | 418 |
419 |
420 | 421 |

Documentation built with MkDocs.

422 |
423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 455 | 456 | 457 | 458 | -------------------------------------------------------------------------------- /site/js/base.js: -------------------------------------------------------------------------------- 1 | function getSearchTerm() 2 | { 3 | var sPageURL = window.location.search.substring(1); 4 | var sURLVariables = sPageURL.split('&'); 5 | for (var i = 0; i < sURLVariables.length; i++) 6 | { 7 | var sParameterName = sURLVariables[i].split('='); 8 | if (sParameterName[0] == 'q') 9 | { 10 | return sParameterName[1]; 11 | } 12 | } 13 | } 14 | 15 | $(document).ready(function() { 16 | 17 | var search_term = getSearchTerm(), 18 | $search_modal = $('#mkdocs_search_modal'); 19 | 20 | if(search_term){ 21 | $search_modal.modal(); 22 | } 23 | 24 | // make sure search input gets autofocus everytime modal opens. 25 | $search_modal.on('shown.bs.modal', function () { 26 | $search_modal.find('#mkdocs-search-query').focus(); 27 | }); 28 | 29 | // Highlight.js 30 | hljs.initHighlightingOnLoad(); 31 | $('table').addClass('table table-striped table-hover'); 32 | }); 33 | 34 | 35 | $('body').scrollspy({ 36 | target: '.bs-sidebar', 37 | }); 38 | 39 | /* Prevent disabled links from causing a page reload */ 40 | $("li.disabled a").click(function() { 41 | event.preventDefault(); 42 | }); 43 | -------------------------------------------------------------------------------- /site/kclass-argument/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | クラスを引数にとる関数をつくる - 逆引きKotlin 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 381 | 382 |
383 | 384 |
392 |
393 | 394 |

クラスを引数にとる関数をつくる

395 |

引数としてクラスを関数に渡したい場合は以下のように書きます。

396 |
fun  <T : Any> jsonToObject(jsonString : String, clazz : KClass<T>) {
397 |     // 処理
398 | }
399 | 
400 |

Kotlinのクラスは"KClass"です。
401 | この関数を使う側は以下のように書きます。

402 |
jsonToObject(jsonString, Hoge::class)
403 | 
404 | 405 |
406 | 407 |
408 |
409 | 410 |

Documentation built with MkDocs.

411 |
412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 444 | 445 | 446 | 447 | -------------------------------------------------------------------------------- /site/license/highlight.js/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2006, Ivan Sagalaev 2 | All rights reserved. 3 | Redistribution and use in source and binary forms, with or without 4 | modification, are permitted provided that the following conditions are met: 5 | 6 | * Redistributions of source code must retain the above copyright 7 | notice, this list of conditions and the following disclaimer. 8 | * Redistributions in binary form must reproduce the above copyright 9 | notice, this list of conditions and the following disclaimer in the 10 | documentation and/or other materials provided with the distribution. 11 | * Neither the name of highlight.js nor the names of its contributors 12 | may be used to endorse or promote products derived from this software 13 | without specific prior written permission. 14 | 15 | THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY 16 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY 19 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | -------------------------------------------------------------------------------- /site/mkdocs/js/mustache.min.js: -------------------------------------------------------------------------------- 1 | (function(global,factory){if(typeof exports==="object"&&exports){factory(exports)}else if(typeof define==="function"&&define.amd){define(["exports"],factory)}else{factory(global.Mustache={})}})(this,function(mustache){var Object_toString=Object.prototype.toString;var isArray=Array.isArray||function(object){return Object_toString.call(object)==="[object Array]"};function isFunction(object){return typeof object==="function"}function escapeRegExp(string){return string.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g,"\\$&")}var RegExp_test=RegExp.prototype.test;function testRegExp(re,string){return RegExp_test.call(re,string)}var nonSpaceRe=/\S/;function isWhitespace(string){return!testRegExp(nonSpaceRe,string)}var entityMap={"&":"&","<":"<",">":">",'"':""","'":"'","/":"/"};function escapeHtml(string){return String(string).replace(/[&<>"'\/]/g,function(s){return entityMap[s]})}var whiteRe=/\s*/;var spaceRe=/\s+/;var equalsRe=/\s*=/;var curlyRe=/\s*\}/;var tagRe=/#|\^|\/|>|\{|&|=|!/;function parseTemplate(template,tags){if(!template)return[];var sections=[];var tokens=[];var spaces=[];var hasTag=false;var nonSpace=false;function stripSpace(){if(hasTag&&!nonSpace){while(spaces.length)delete tokens[spaces.pop()]}else{spaces=[]}hasTag=false;nonSpace=false}var openingTagRe,closingTagRe,closingCurlyRe;function compileTags(tags){if(typeof tags==="string")tags=tags.split(spaceRe,2);if(!isArray(tags)||tags.length!==2)throw new Error("Invalid tags: "+tags);openingTagRe=new RegExp(escapeRegExp(tags[0])+"\\s*");closingTagRe=new RegExp("\\s*"+escapeRegExp(tags[1]));closingCurlyRe=new RegExp("\\s*"+escapeRegExp("}"+tags[1]))}compileTags(tags||mustache.tags);var scanner=new Scanner(template);var start,type,value,chr,token,openSection;while(!scanner.eos()){start=scanner.pos;value=scanner.scanUntil(openingTagRe);if(value){for(var i=0,valueLength=value.length;i0?sections[sections.length-1][4]:nestedTokens;break;default:collector.push(token)}}return nestedTokens}function Scanner(string){this.string=string;this.tail=string;this.pos=0}Scanner.prototype.eos=function(){return this.tail===""};Scanner.prototype.scan=function(re){var match=this.tail.match(re);if(!match||match.index!==0)return"";var string=match[0];this.tail=this.tail.substring(string.length);this.pos+=string.length;return string};Scanner.prototype.scanUntil=function(re){var index=this.tail.search(re),match;switch(index){case-1:match=this.tail;this.tail="";break;case 0:match="";break;default:match=this.tail.substring(0,index);this.tail=this.tail.substring(index)}this.pos+=match.length;return match};function Context(view,parentContext){this.view=view;this.cache={".":this.view};this.parent=parentContext}Context.prototype.push=function(view){return new Context(view,this)};Context.prototype.lookup=function(name){var cache=this.cache;var value;if(name in cache){value=cache[name]}else{var context=this,names,index,lookupHit=false;while(context){if(name.indexOf(".")>0){value=context.view;names=name.split(".");index=0;while(value!=null&&index")value=this._renderPartial(token,context,partials,originalTemplate);else if(symbol==="&")value=this._unescapedValue(token,context);else if(symbol==="name")value=this._escapedValue(token,context);else if(symbol==="text")value=this._rawValue(token);if(value!==undefined)buffer+=value}return buffer};Writer.prototype._renderSection=function(token,context,partials,originalTemplate){var self=this;var buffer="";var value=context.lookup(token[1]);function subRender(template){return self.render(template,context,partials)}if(!value)return;if(isArray(value)){for(var j=0,valueLength=value.length;j 2 |

{{title}}

3 |

{{summary}}

4 | 5 | -------------------------------------------------------------------------------- /site/mkdocs/js/search.js: -------------------------------------------------------------------------------- 1 | require([ 2 | base_url + '/mkdocs/js/mustache.min.js', 3 | base_url + '/mkdocs/js/lunr.min.js', 4 | 'text!search-results-template.mustache', 5 | 'text!../search_index.json', 6 | ], function (Mustache, lunr, results_template, data) { 7 | "use strict"; 8 | 9 | function getSearchTerm() 10 | { 11 | var sPageURL = window.location.search.substring(1); 12 | var sURLVariables = sPageURL.split('&'); 13 | for (var i = 0; i < sURLVariables.length; i++) 14 | { 15 | var sParameterName = sURLVariables[i].split('='); 16 | if (sParameterName[0] == 'q') 17 | { 18 | return decodeURIComponent(sParameterName[1].replace(/\+/g, '%20')); 19 | } 20 | } 21 | } 22 | 23 | var index = lunr(function () { 24 | this.field('title', {boost: 10}); 25 | this.field('text'); 26 | this.ref('location'); 27 | }); 28 | 29 | data = JSON.parse(data); 30 | var documents = {}; 31 | 32 | for (var i=0; i < data.docs.length; i++){ 33 | var doc = data.docs[i]; 34 | doc.location = base_url + doc.location; 35 | index.add(doc); 36 | documents[doc.location] = doc; 37 | } 38 | 39 | var search = function(){ 40 | 41 | var query = document.getElementById('mkdocs-search-query').value; 42 | var search_results = document.getElementById("mkdocs-search-results"); 43 | while (search_results.firstChild) { 44 | search_results.removeChild(search_results.firstChild); 45 | } 46 | 47 | if(query === ''){ 48 | return; 49 | } 50 | 51 | var results = index.search(query); 52 | 53 | if (results.length > 0){ 54 | for (var i=0; i < results.length; i++){ 55 | var result = results[i]; 56 | doc = documents[result.ref]; 57 | doc.base_url = base_url; 58 | doc.summary = doc.text.substring(0, 200); 59 | var html = Mustache.to_html(results_template, doc); 60 | search_results.insertAdjacentHTML('beforeend', html); 61 | } 62 | } else { 63 | search_results.insertAdjacentHTML('beforeend', "

No results found

"); 64 | } 65 | 66 | if(jQuery){ 67 | /* 68 | * We currently only automatically hide bootstrap models. This 69 | * requires jQuery to work. 70 | */ 71 | jQuery('#mkdocs_search_modal a').click(function(){ 72 | jQuery('#mkdocs_search_modal').modal('hide'); 73 | }); 74 | } 75 | 76 | }; 77 | 78 | var search_input = document.getElementById('mkdocs-search-query'); 79 | 80 | var term = getSearchTerm(); 81 | if (term){ 82 | search_input.value = term; 83 | search(); 84 | } 85 | 86 | search_input.addEventListener("keyup", search); 87 | 88 | }); 89 | -------------------------------------------------------------------------------- /site/nav-sub.html: -------------------------------------------------------------------------------- 1 | {% if not nav_item.children %} 2 |
  • 3 | {{ nav_item.title }} 4 |
  • 5 | {% else %} 6 | 14 | {% endif %} 15 | -------------------------------------------------------------------------------- /site/nav.html: -------------------------------------------------------------------------------- 1 | 78 | -------------------------------------------------------------------------------- /site/sitemap.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | http://kotlin-rev-solution.herokuapp.com/site/ 7 | 2019-03-15 8 | daily 9 | 10 | 11 | 12 | 13 | 14 | 15 | http://kotlin-rev-solution.herokuapp.com/site/kotlin-install/ 16 | 2019-03-15 17 | daily 18 | 19 | 20 | 21 | http://kotlin-rev-solution.herokuapp.com/site/kotlin-plugin-intellij/ 22 | 2019-03-15 23 | daily 24 | 25 | 26 | 27 | http://kotlin-rev-solution.herokuapp.com/site/kotlin-plugin-androidstudio/ 28 | 2019-03-15 29 | daily 30 | 31 | 32 | 33 | http://kotlin-rev-solution.herokuapp.com/site/kotlin-plugin-eclipse/ 34 | 2019-03-15 35 | daily 36 | 37 | 38 | 39 | http://kotlin-rev-solution.herokuapp.com/site/hello-kotlin-android/ 40 | 2019-03-15 41 | daily 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | http://kotlin-rev-solution.herokuapp.com/site/variable/ 50 | 2019-03-15 51 | daily 52 | 53 | 54 | 55 | http://kotlin-rev-solution.herokuapp.com/site/type/ 56 | 2019-03-15 57 | daily 58 | 59 | 60 | 61 | http://kotlin-rev-solution.herokuapp.com/site/instance/ 62 | 2019-03-15 63 | daily 64 | 65 | 66 | 67 | http://kotlin-rev-solution.herokuapp.com/site/array/ 68 | 2019-03-15 69 | daily 70 | 71 | 72 | 73 | http://kotlin-rev-solution.herokuapp.com/site/function/ 74 | 2019-03-15 75 | daily 76 | 77 | 78 | 79 | http://kotlin-rev-solution.herokuapp.com/site/function_type/ 80 | 2019-03-15 81 | daily 82 | 83 | 84 | 85 | http://kotlin-rev-solution.herokuapp.com/site/function_object/ 86 | 2019-03-15 87 | daily 88 | 89 | 90 | 91 | http://kotlin-rev-solution.herokuapp.com/site/if-statement/ 92 | 2019-03-15 93 | daily 94 | 95 | 96 | 97 | http://kotlin-rev-solution.herokuapp.com/site/if-function/ 98 | 2019-03-15 99 | daily 100 | 101 | 102 | 103 | http://kotlin-rev-solution.herokuapp.com/site/when/ 104 | 2019-03-15 105 | daily 106 | 107 | 108 | 109 | http://kotlin-rev-solution.herokuapp.com/site/loop/ 110 | 2019-03-15 111 | daily 112 | 113 | 114 | 115 | http://kotlin-rev-solution.herokuapp.com/site/enum/ 116 | 2019-03-15 117 | daily 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | http://kotlin-rev-solution.herokuapp.com/site/kotlin-collection-framework/ 126 | 2019-03-15 127 | daily 128 | 129 | 130 | 131 | http://kotlin-rev-solution.herokuapp.com/site/collection-for-each/ 132 | 2019-03-15 133 | daily 134 | 135 | 136 | 137 | http://kotlin-rev-solution.herokuapp.com/site/collection-filter/ 138 | 2019-03-15 139 | daily 140 | 141 | 142 | 143 | http://kotlin-rev-solution.herokuapp.com/site/collection-map/ 144 | 2019-03-15 145 | daily 146 | 147 | 148 | 149 | http://kotlin-rev-solution.herokuapp.com/site/collection-merge/ 150 | 2019-03-15 151 | daily 152 | 153 | 154 | 155 | http://kotlin-rev-solution.herokuapp.com/site/collection-sum/ 156 | 2019-03-15 157 | daily 158 | 159 | 160 | 161 | http://kotlin-rev-solution.herokuapp.com/site/collection-drop/ 162 | 2019-03-15 163 | daily 164 | 165 | 166 | 167 | http://kotlin-rev-solution.herokuapp.com/site/collection-take/ 168 | 2019-03-15 169 | daily 170 | 171 | 172 | 173 | http://kotlin-rev-solution.herokuapp.com/site/collection-zip/ 174 | 2019-03-15 175 | daily 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | http://kotlin-rev-solution.herokuapp.com/site/extension/ 184 | 2019-03-15 185 | daily 186 | 187 | 188 | 189 | http://kotlin-rev-solution.herokuapp.com/site/type-safe-builder/ 190 | 2019-03-15 191 | daily 192 | 193 | 194 | 195 | http://kotlin-rev-solution.herokuapp.com/site/reflection/ 196 | 2019-03-15 197 | daily 198 | 199 | 200 | 201 | http://kotlin-rev-solution.herokuapp.com/site/framework-for-kotlin/ 202 | 2019-03-15 203 | daily 204 | 205 | 206 | 207 | http://kotlin-rev-solution.herokuapp.com/site/unit-test-for-kotlin/ 208 | 2019-03-15 209 | daily 210 | 211 | 212 | 213 | http://kotlin-rev-solution.herokuapp.com/site/keyword-for-kotlin/ 214 | 2019-03-15 215 | daily 216 | 217 | 218 | 219 | http://kotlin-rev-solution.herokuapp.com/site/orm-for-kotlin/ 220 | 2019-03-15 221 | daily 222 | 223 | 224 | 225 | http://kotlin-rev-solution.herokuapp.com/site/try-with-resources-for-kotlin/ 226 | 2019-03-15 227 | daily 228 | 229 | 230 | 231 | http://kotlin-rev-solution.herokuapp.com/site/todo/ 232 | 2019-03-15 233 | daily 234 | 235 | 236 | 237 | http://kotlin-rev-solution.herokuapp.com/site/kclass-argument/ 238 | 2019-03-15 239 | daily 240 | 241 | 242 | 243 | http://kotlin-rev-solution.herokuapp.com/site/gradle-kts/ 244 | 2019-03-15 245 | daily 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | http://kotlin-rev-solution.herokuapp.com/site/type-alias/ 254 | 2019-03-15 255 | daily 256 | 257 | 258 | 259 | http://kotlin-rev-solution.herokuapp.com/site/coroutines-for-kotlin/ 260 | 2019-03-15 261 | daily 262 | 263 | 264 | 265 | http://kotlin-rev-solution.herokuapp.com/site/all-open-plugin/ 266 | 2019-03-15 267 | daily 268 | 269 | 270 | 271 | 272 | -------------------------------------------------------------------------------- /site/toc.html: -------------------------------------------------------------------------------- 1 | 11 | -------------------------------------------------------------------------------- /site/try-with-resources-for-kotlin/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | Kotlinでtry-with-resourcesが使いたい - 逆引きKotlin 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 381 | 382 |
    383 | 384 |
    392 |
    393 | 394 |

    Kotlinでtry-with-resourcesが使いたい

    395 |

    useを使います。

    396 |
    val inputStream = Thread.currentThread().contextClassLoader.getResourceAsStream("hogehoge")
    397 | java.io.InputStreamReader(inputStream).use {
    398 |     // `it`がInputStreamReaderのインスタンス
    399 |     val b = it.read()
    400 | }
    401 | 
    402 |

    useはClosableインターフェースを実装しているクラスの拡張関数として提供されています。 403 | したがってcloseが実装されているクラスには同じように適用できます。

    404 |

    https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/use.html

    405 | 406 |
    407 | 408 |
    409 |
    410 | 411 |

    Documentation built with MkDocs.

    412 |
    413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 445 | 446 | 447 | 448 | -------------------------------------------------------------------------------- /themes/404.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block content %} 4 | 5 |
    6 |
    7 |

    404

    8 |

    Page not found

    9 |
    10 |
    11 | 12 | {% endblock %} 13 | -------------------------------------------------------------------------------- /themes/base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | {% if page_description %}{% endif %} 8 | {% if site_author %}{% endif %} 9 | {% if canonical_url %}{% endif %} 10 | {% if favicon %} 11 | {% else %}{% endif %} 12 | 13 | {% if page_title %}{{ page_title }} - {% endif %}{{ site_name }} 14 | 15 | 16 | 17 | 18 | 19 | {%- for path in extra_css %} 20 | 21 | {%- endfor %} 22 | 23 | 24 | 28 | 29 | {% if google_analytics %} 30 | 39 | {% endif %} 40 | 41 | 42 | 43 | 44 | {% include "nav.html" %} 45 | 46 |
    47 | {% block content %} 48 |
    {% include "toc.html" %}
    49 |
    {% include "content.html" %}
    50 | {% endblock %} 51 |
    52 | 53 |
    54 |
    55 | {% if copyright %} 56 |

    {{ copyright }}

    57 | {% endif %} 58 |

    Documentation built with MkDocs.

    59 |
    60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | {%- for path in extra_javascript %} 68 | 69 | {%- endfor %} 70 | 71 | 95 | 96 | 97 | 98 | {% if current_page and current_page.is_homepage %} 99 | 103 | {% endif %} 104 | -------------------------------------------------------------------------------- /themes/content.html: -------------------------------------------------------------------------------- 1 | {% if meta.source %} 2 | 7 | {% endif %} 8 | 9 | {{ content }} 10 | -------------------------------------------------------------------------------- /themes/css/base.css: -------------------------------------------------------------------------------- 1 | body { 2 | padding-top: 70px; 3 | background: url(../img/grid.png) repeat-x; 4 | background-attachment: fixed; 5 | background-color: #f8f8f8; 6 | } 7 | 8 | body > div.container { 9 | min-height: 400px; 10 | } 11 | 12 | ul.nav li.main { 13 | font-weight: bold; 14 | } 15 | 16 | div.col-md-3 { 17 | padding-left: 0; 18 | } 19 | 20 | div.col-md-9 { 21 | padding-bottom: 100px; 22 | } 23 | 24 | div.source-links { 25 | float: right; 26 | } 27 | 28 | div.col-md-9 img { 29 | max-width: 100%; 30 | display: inline; 31 | padding: 4px; 32 | line-height: 1.428571429; 33 | background-color: #fff; 34 | border: 1px solid #ddd; 35 | border-radius: 4px; 36 | margin: 20px auto 30px auto; 37 | } 38 | 39 | h1, h2, h3 { 40 | color: #444; 41 | } 42 | 43 | h1[id]:before, h2[id]:before, h3[id]:before, h4[id]:before, h5[id]:before, h6[id]:before { 44 | content: ""; 45 | display: block; 46 | margin-top: -75px; 47 | height: 75px; 48 | pointer-events: none; 49 | } 50 | 51 | h1 { 52 | font-weight: 400; 53 | font-size: 42px; 54 | } 55 | 56 | h2, h3, h4, h5, h6 { 57 | font-weight: 300; 58 | } 59 | 60 | hr { 61 | border-top: 1px solid #aaa; 62 | } 63 | 64 | pre, .rst-content tt { 65 | max-width: 100%; 66 | background: #fff; 67 | border: solid 1px #e1e4e5; 68 | color: #e74c3c; 69 | overflow-x: auto; 70 | } 71 | 72 | code.code-large, .rst-content tt.code-large { 73 | font-size: 90%; 74 | } 75 | 76 | code { 77 | padding: 2px 5px; 78 | color: #c7254e; 79 | background-color: #f9f2f4; 80 | font-size: 75%; 81 | } 82 | 83 | code, kbd, pre, samp { 84 | font-family: monospace,serif; 85 | font-size: 12px !important; 86 | } 87 | 88 | footer { 89 | margin-top: 30px; 90 | margin-bottom: 10px; 91 | text-align: center; 92 | font-weight: 200; 93 | } 94 | 95 | .modal-dialog { 96 | margin-top: 60px; 97 | } 98 | 99 | /* 100 | * Side navigation 101 | * 102 | * Scrollspy and affixed enhanced navigation to highlight sections and secondary 103 | * sections of docs content. 104 | */ 105 | 106 | /* By default it's not affixed in mobile views, so undo that */ 107 | .bs-sidebar.affix { 108 | position: static; 109 | } 110 | 111 | .bs-sidebar.well { 112 | padding: 0; 113 | max-height: 90%; 114 | overflow-y: auto; 115 | } 116 | 117 | /* First level of nav */ 118 | .bs-sidenav { 119 | padding-top: 10px; 120 | padding-bottom: 10px; 121 | border-radius: 5px; 122 | } 123 | 124 | /* All levels of nav */ 125 | .bs-sidebar .nav > li > a { 126 | display: block; 127 | padding: 5px 20px; 128 | z-index: 1; 129 | } 130 | .bs-sidebar .nav > li > a:hover, 131 | .bs-sidebar .nav > li > a:focus { 132 | text-decoration: none; 133 | border-right: 1px solid; 134 | } 135 | .bs-sidebar .nav > .active > a, 136 | .bs-sidebar .nav > .active:hover > a, 137 | .bs-sidebar .nav > .active:focus > a { 138 | font-weight: bold; 139 | background-color: transparent; 140 | border-right: 1px solid; 141 | } 142 | 143 | /* Nav: second level (shown on .active) */ 144 | .bs-sidebar .nav .nav { 145 | display: none; /* Hide by default, but at >768px, show it */ 146 | margin-bottom: 8px; 147 | } 148 | .bs-sidebar .nav .nav > li > a { 149 | padding-top: 3px; 150 | padding-bottom: 3px; 151 | padding-left: 30px; 152 | font-size: 90%; 153 | } 154 | 155 | /* Show and affix the side nav when space allows it */ 156 | @media (min-width: 992px) { 157 | .bs-sidebar .nav > .active > ul { 158 | display: block; 159 | } 160 | /* Widen the fixed sidebar */ 161 | .bs-sidebar.affix, 162 | .bs-sidebar.affix-bottom { 163 | width: 213px; 164 | } 165 | .bs-sidebar.affix { 166 | position: fixed; /* Undo the static from mobile first approach */ 167 | top: 80px; 168 | } 169 | .bs-sidebar.affix-bottom { 170 | position: absolute; /* Undo the static from mobile first approach */ 171 | } 172 | .bs-sidebar.affix-bottom .bs-sidenav, 173 | .bs-sidebar.affix .bs-sidenav { 174 | margin-top: 0; 175 | margin-bottom: 0; 176 | } 177 | } 178 | @media (min-width: 1200px) { 179 | /* Widen the fixed sidebar again */ 180 | .bs-sidebar.affix-bottom, 181 | .bs-sidebar.affix { 182 | width: 263px; 183 | } 184 | } 185 | 186 | .headerlink { 187 | font-family: FontAwesome; 188 | font-size: 14px; 189 | display: none; 190 | padding-left: .5em; 191 | } 192 | 193 | h1:hover .headerlink, h2:hover .headerlink, h3:hover .headerlink, h4:hover .headerlink, h5:hover .headerlink, h6:hover .headerlink{ 194 | display:inline-block; 195 | } 196 | 197 | 198 | 199 | .admonition { 200 | padding: 15px; 201 | margin-bottom: 20px; 202 | border: 1px solid transparent; 203 | border-radius: 4px; 204 | text-align: left; 205 | } 206 | 207 | .admonition.note { 208 | color: #3a87ad; 209 | background-color: #d9edf7; 210 | border-color: #bce8f1; 211 | } 212 | 213 | .admonition.warning { 214 | color: #c09853; 215 | background-color: #fcf8e3; 216 | border-color: #fbeed5; 217 | } 218 | 219 | .admonition.danger { 220 | color: #b94a48; 221 | background-color: #f2dede; 222 | border-color: #eed3d7; 223 | } 224 | 225 | .admonition-title { 226 | font-weight: bold; 227 | text-align: left; 228 | } 229 | 230 | 231 | .dropdown-submenu { 232 | position: relative; 233 | } 234 | 235 | .dropdown-submenu>.dropdown-menu { 236 | top: 0; 237 | left: 100%; 238 | margin-top: -6px; 239 | margin-left: -1px; 240 | -webkit-border-radius: 0 6px 6px 6px; 241 | -moz-border-radius: 0 6px 6px; 242 | border-radius: 0 6px 6px 6px; 243 | } 244 | 245 | .dropdown-submenu:hover>.dropdown-menu { 246 | display: block; 247 | } 248 | 249 | .dropdown-submenu>a:after { 250 | display: block; 251 | content: " "; 252 | float: right; 253 | width: 0; 254 | height: 0; 255 | border-color: transparent; 256 | border-style: solid; 257 | border-width: 5px 0 5px 5px; 258 | border-left-color: #ccc; 259 | margin-top: 5px; 260 | margin-right: -10px; 261 | } 262 | 263 | .dropdown-submenu:hover>a:after { 264 | border-left-color: #fff; 265 | } 266 | 267 | .dropdown-submenu.pull-left { 268 | float: none; 269 | } 270 | 271 | .dropdown-submenu.pull-left>.dropdown-menu { 272 | left: -100%; 273 | margin-left: 10px; 274 | -webkit-border-radius: 6px 0 6px 6px; 275 | -moz-border-radius: 6px 0 6px 6px; 276 | border-radius: 6px 0 6px 6px; 277 | } 278 | -------------------------------------------------------------------------------- /themes/css/highlight.css: -------------------------------------------------------------------------------- 1 | /* 2 | This is the GitHub theme for highlight.js 3 | 4 | github.com style (c) Vasily Polovnyov 5 | 6 | */ 7 | 8 | .hljs { 9 | display: block; 10 | overflow-x: auto; 11 | padding: 0.5em; 12 | color: #333; 13 | -webkit-text-size-adjust: none; 14 | } 15 | 16 | .hljs-comment, 17 | .diff .hljs-header, 18 | .hljs-javadoc { 19 | color: #998; 20 | font-style: italic; 21 | } 22 | 23 | .hljs-keyword, 24 | .css .rule .hljs-keyword, 25 | .hljs-winutils, 26 | .nginx .hljs-title, 27 | .hljs-subst, 28 | .hljs-request, 29 | .hljs-status { 30 | color: #333; 31 | font-weight: bold; 32 | } 33 | 34 | .hljs-number, 35 | .hljs-hexcolor, 36 | .ruby .hljs-constant { 37 | color: #008080; 38 | } 39 | 40 | .hljs-string, 41 | .hljs-tag .hljs-value, 42 | .hljs-phpdoc, 43 | .hljs-dartdoc, 44 | .tex .hljs-formula { 45 | color: #d14; 46 | } 47 | 48 | .hljs-title, 49 | .hljs-id, 50 | .scss .hljs-preprocessor { 51 | color: #900; 52 | font-weight: bold; 53 | } 54 | 55 | .hljs-list .hljs-keyword, 56 | .hljs-subst { 57 | font-weight: normal; 58 | } 59 | 60 | .hljs-class .hljs-title, 61 | .hljs-type, 62 | .vhdl .hljs-literal, 63 | .tex .hljs-command { 64 | color: #458; 65 | font-weight: bold; 66 | } 67 | 68 | .hljs-tag, 69 | .hljs-tag .hljs-title, 70 | .hljs-rule .hljs-property, 71 | .django .hljs-tag .hljs-keyword { 72 | color: #000080; 73 | font-weight: normal; 74 | } 75 | 76 | .hljs-attribute, 77 | .hljs-variable, 78 | .lisp .hljs-body, 79 | .hljs-name { 80 | color: #008080; 81 | } 82 | 83 | .hljs-regexp { 84 | color: #009926; 85 | } 86 | 87 | .hljs-symbol, 88 | .ruby .hljs-symbol .hljs-string, 89 | .lisp .hljs-keyword, 90 | .clojure .hljs-keyword, 91 | .scheme .hljs-keyword, 92 | .tex .hljs-special, 93 | .hljs-prompt { 94 | color: #990073; 95 | } 96 | 97 | .hljs-built_in { 98 | color: #0086b3; 99 | } 100 | 101 | .hljs-preprocessor, 102 | .hljs-pragma, 103 | .hljs-pi, 104 | .hljs-doctype, 105 | .hljs-shebang, 106 | .hljs-cdata { 107 | color: #999; 108 | font-weight: bold; 109 | } 110 | 111 | .hljs-deletion { 112 | background: #fdd; 113 | } 114 | 115 | .hljs-addition { 116 | background: #dfd; 117 | } 118 | 119 | .diff .hljs-change { 120 | background: #0086b3; 121 | } 122 | 123 | .hljs-chunk { 124 | color: #aaa; 125 | } 126 | -------------------------------------------------------------------------------- /themes/fonts/fontawesome-webfont.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yyYank/kotlin-rev-solution/6369e30949e503abac9627ff21c5b1688b4e4fdd/themes/fonts/fontawesome-webfont.eot -------------------------------------------------------------------------------- /themes/fonts/fontawesome-webfont.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yyYank/kotlin-rev-solution/6369e30949e503abac9627ff21c5b1688b4e4fdd/themes/fonts/fontawesome-webfont.ttf -------------------------------------------------------------------------------- /themes/fonts/fontawesome-webfont.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yyYank/kotlin-rev-solution/6369e30949e503abac9627ff21c5b1688b4e4fdd/themes/fonts/fontawesome-webfont.woff -------------------------------------------------------------------------------- /themes/img/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yyYank/kotlin-rev-solution/6369e30949e503abac9627ff21c5b1688b4e4fdd/themes/img/favicon.ico -------------------------------------------------------------------------------- /themes/img/grid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yyYank/kotlin-rev-solution/6369e30949e503abac9627ff21c5b1688b4e4fdd/themes/img/grid.png -------------------------------------------------------------------------------- /themes/js/base.js: -------------------------------------------------------------------------------- 1 | function getSearchTerm() 2 | { 3 | var sPageURL = window.location.search.substring(1); 4 | var sURLVariables = sPageURL.split('&'); 5 | for (var i = 0; i < sURLVariables.length; i++) 6 | { 7 | var sParameterName = sURLVariables[i].split('='); 8 | if (sParameterName[0] == 'q') 9 | { 10 | return sParameterName[1]; 11 | } 12 | } 13 | } 14 | 15 | $(document).ready(function() { 16 | 17 | var search_term = getSearchTerm(), 18 | $search_modal = $('#mkdocs_search_modal'); 19 | 20 | if(search_term){ 21 | $search_modal.modal(); 22 | } 23 | 24 | // make sure search input gets autofocus everytime modal opens. 25 | $search_modal.on('shown.bs.modal', function () { 26 | $search_modal.find('#mkdocs-search-query').focus(); 27 | }); 28 | 29 | // Highlight.js 30 | hljs.initHighlightingOnLoad(); 31 | $('table').addClass('table table-striped table-hover'); 32 | }); 33 | 34 | 35 | $('body').scrollspy({ 36 | target: '.bs-sidebar', 37 | }); 38 | 39 | /* Prevent disabled links from causing a page reload */ 40 | $("li.disabled a").click(function() { 41 | event.preventDefault(); 42 | }); 43 | -------------------------------------------------------------------------------- /themes/license/highlight.js/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2006, Ivan Sagalaev 2 | All rights reserved. 3 | Redistribution and use in source and binary forms, with or without 4 | modification, are permitted provided that the following conditions are met: 5 | 6 | * Redistributions of source code must retain the above copyright 7 | notice, this list of conditions and the following disclaimer. 8 | * Redistributions in binary form must reproduce the above copyright 9 | notice, this list of conditions and the following disclaimer in the 10 | documentation and/or other materials provided with the distribution. 11 | * Neither the name of highlight.js nor the names of its contributors 12 | may be used to endorse or promote products derived from this software 13 | without specific prior written permission. 14 | 15 | THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY 16 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY 19 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | -------------------------------------------------------------------------------- /themes/nav-sub.html: -------------------------------------------------------------------------------- 1 | {% if not nav_item.children %} 2 |
  • 3 | {{ nav_item.title }} 4 |
  • 5 | {% else %} 6 | 14 | {% endif %} 15 | -------------------------------------------------------------------------------- /themes/nav.html: -------------------------------------------------------------------------------- 1 | 78 | -------------------------------------------------------------------------------- /themes/toc.html: -------------------------------------------------------------------------------- 1 | 11 | --------------------------------------------------------------------------------