12æã«ãªãã¾ããã2015å¹´ãæ®ãã¨ãã1ã¶æã§ããã ã¨ãããã¨ã§Kotlin Advent Calendar 2015ã®ç¬¬1æ¥ç®ã®è¨äºã§ãÙ©( 'Ï' )Ù
å¹´å ã«Kotlin 1.0ããªãªã¼ã¹ãããã¨ã®åã§ãããç¾ãã¼ã¸ã§ã³ã¯1.0-Beta2ã§ãæ£å¼çãªãªã¼ã¹ã®è¶³é³ãèããã¦ãã¾ããã æ£å¼çã®ãªãªã¼ã¹ã¯ãKotlinã¦ã¼ã¶ãå¾ ã¡ã«å¾ ã£ãç¬éã§ãã ãã¡ããç§ããã®ä¸äººã§ãã ç¹ã«Kotlinã¨ãã³ã¸ã§ãªã¹ããèªç§°ãã以åããæ´»åãã¦ãããã¨ããã£ã¦ãã®æåã¯ã²ã¨ããã§ãããã æ¬ã¨ã³ããªã§ã¯Kotlinã®æãåºãæç³»åã§ç¶´ã£ã¦ããããã¨æãã¾ãã
ãããããæ£å¼çããªãªã¼ã¹ãããã¨ãã«ã¯ã¡ãã£ã¨ããèªçãã¼ãã£ã¼ãéå¬ããäºå®ã§ãã ã¨è¨ã£ã¦ãå 容ã¯ãã¢ããã·ã¥ãªLT大ä¼ã§ãã ããã§ãä¼¼ããããªè©±ãããã¨æãã¾ãã
Kotlinã®ç»å ´ã¨åºä¼ã
Kotlinã¯ãã·ã¢ã®å³¶ã®ååã§ããããããããã°ã©ãã³ã°è¨èª(ã¨ãã®éçºããã¸ã§ã¯ã)ã®ååã¨ãªã£ã¦çºè¡¨ãããã®ã¯ä»ãã4å¹´åã®2011å¹´ã7æã®ãã¨ã§ããã ã«ãªãã©ã«ãã¢ã§éãããJVM Language Summitã®ä¸ã§ãIntelliJ IDEAãªã©ã®IDEã§ããªãã¿ã®JetBrains社ã«ããçºè¡¨ãããã®ã§ãã å½æã®åç»ã¯å ã®ãªã³ã¯ãããã¹ã©ã¤ãã¯å ¬å¼ããã°è¨äºããè¦ãã¾ãã
ç§ã¯ãã®ã¤ãã³ãããJetBrainsã®ååããã§ãã¯ãã¦ããªãã£ãã®ã§Kotlinã¨åºä¼ã£ãã®ã¯ããå°ãå¾ã§ããã æåã«Kotlinãç¥ã£ãã®ã¯ãã¶ããã®è¨äºã 第ä¸å°è±¡ã¯ãã¾ãæ°ããè¨èªã...ãã¨ãããããååã ããç´ ç´ãªææ³ã ãªã¼ããéçåä»ãã ï¼ããJVMè¨èªã ã試ãããããã§ããã
ã§ããã¤ãã®ç§ã§ããããã£ã¨ãµã³ãã«ã³ã¼ãçºãã¦ãã¡ããã£ã¨åããã¦ã¿ã¦ãçµäºï¼ã¨ããæãã ã¨æãã®ã§ãããä»åã¯éãã¾ããã è¨èªã¨ãã¦é¢ç½ããã§ã´ã¼ã«ã»è¨è¨ææ³ã«å ±æã§ãããã¨ãç»å ´ãã¦éããªããã¨ãæ¥æ¬èªæ å ±ãã»ã¼çç¡ã§ãã£ããã¨ãã¾ããã«ãã£ã¦ããããªäººãããªãã£ããã¨ãªã©ãããããã£ã¦ãããï¼Kotlin極ãããï¼ãã¨æãç«ã£ãã®ã§ããã
Kotlinã³ã³ãã¤ã©ã®ãªãªã¼ã¹
ç§ãKotlinã¨åºä¼ã£ãå½åãè¨èªã®æ©è½ãææ³ã説æããç°¡åãªããã¥ã¡ã³ãã¯ããã¾ãããã³ã³ãã¤ã©ãåä½ç°å¢ãããã¾ããã§ããã ãã¯ãåãããªãã¨æ¥½ããããã¾ããããã²ãããããã¥ã¡ã³ããèªãã§ãã¾ããã 2012å¹´2æä¸æ¬ã«Kotlinã³ã³ãã¤ã©ã¨ãWebãã©ã¦ã¶ä¸ã§åä½ããå½æWeb Demoã¨å¼ã°ããç°å¢ï¼ä»ã®try.kotlinlang.orgã¿ãããªãã¤ï¼ããªãªã¼ã¹ããã¾ããã
ããã§å°ãæã®Kotlinã®ææ³ã»æ©è½ã«ã¤ãã¦ç´¹ä»ãã¾ãã
ä»ã®å§¿ã¨å¤§ããå¤ãã£ã¦ã¯ãã¾ããããå°ããå¤æ´ã¯ããããããã¾ãã
ä¾ãã°ã©ã ãå¼ãwhenå¼ã®->
ã¯ãåæã¯=>
ã§ããã
ããããããã¤ã¦ã¯ã¿ãã«ãããã¾ããã
// ã¿ãã«åã¨ã¿ãã«ãªãã©ã« val pair: #(String, Int) = #("foo", 123) pair._1 //=> "foo"
ãã®ã»ããé¢æ°åã®è¨æ³ãfun (A): B
ãã(A) -> B
ã«å¤ãã£ãããã¾ããã
ã³ã³ããªãã¥ã¼ã: ã¨ãã³ã¸ã§ãªã¹ãã¸ã®ç¬¬ä¸æ©
2012å¹´æ¥é ãKotlinã«ã¤ãã¦ãããã調ã¹ã¦ã¯ããã°ã«æ¸ããã¨ããã®ãç¹°ãè¿ãã¦ãã¾ããã ç§ã®è¨å¿µãã¹ãKotlinãã¬ã¼ã³ç¬¬1åç®ã¯Twitter APIåå¼·ä¼ã§ããï¼ãç²æ«ãªããã¹ã©ã¤ãã¯ãã¡ãï¼ã
ãã®é ãKotlinæ¨æºã©ã¤ãã©ãªã¸ã®ã³ãããããã¾ããã
確ãString
ã ã£ãããªãã®æ¡å¼µé¢æ°ããã¡ããã¡ãä½ã£ã¦ãã«ãªã¯éã£ããåãè¾¼ã¾ãã¾ãã*1ã
å½æã®å
¬å¼ããã¥ã¡ã³ããããã°ã§Taro Nagasawaã¨ååãè¼ãã¦ããã ãã¾ããã
ãM5: æ´ç·´ããã¦ããKotlin
Kotlinã¯ããã¤ã«ã¹ãã¼ã³ã¨ããå½¢ã§éçºä¸ã®ã¹ãããã·ã§ããããæ°ã¶æã®ã¹ãã³ã§ãªãªã¼ã¹ããã¦æ¥ã¾ããã ããããã®ãã¤ã«ã¹ãã¼ã³ã§ã¯ãæ©è½ã®è¿½å ã»å»æ¢ãææ³ã®å¤æ´ãããã©ã¼ãã³ã¹æ¹åããã°ä¿®æ£ãªã©ãå«ã¾ããæ éãã¤å¤§èã«Kotlinã¯æé·ãéãã¦æ¥ã¾ããã æ°ãããã¤ã«ã¹ãã¼ã³ãå ¬éããããã³ã«ã¯ã¯ã¯ã¯ããªãããªãªã¼ã¹ãã¼ããèªãã§ãããã®ã§ãã
M1ã®ãããªåæã®é ã¯æªå®è£ ã®æ©è½ã追å ããã¦è¡ãã¾ããã ä¾ãã°ãã¼ã«ã«é¢æ°ã§ãã£ããåæåãã¢ããã¼ã·ã§ã³ãªã©ãå®è£ ããã¾ããã
M2ã§Kotlinã®Androidãµãã¼ããçºè¡¨ããã¾ããã
ãããããinvoke
ã¡ã½ããã§ãããã®ã¡ã½ãããæã£ããªãã¸ã§ã¯ãã¸ã®åç
§ãä¾ãã°foo
ã¨ããã¨ãfoo.invoke()
ã®ä»£ããã«foo()
ã¨è¨è¿°ãã¦å¼ã³åºããããã«ãªãã¾ããã
M3ã§ã¯å¤§ããªå¤æ´ãããã¤ãããã¾ããã
ã¿ãã«ãå»æ¢ããã¾ããã代ããã«ãã¼ã¿ã¯ã©ã¹ã¨å¤é代å
¥ãå°å
¥ããã¾ããã
ãã¼ã«ã«ã«ï¼ã¤ã¾ãé¢æ°å
ã«ï¼class
ãobject
ãå®ç¾©ã§ããããã«ãªãã¾ããã
ã³ã¬ã¯ã·ã§ã³APIãæ¹åããã¾ããï¼ãã¥ã¼ã¿ãã«ã¨ã¤ãã¥ã¼ã¿ãã«ãåºå¥ãããªã©ï¼ã
ã»ã«ã³ããªã³ã³ã¹ãã©ã¯ã¿ãå»æ¢ããã¾ããï¼ãã¶ããã®ãã¼ã¸ã§ã³ã®ã¨ãï¼ã
ããã¦ãsure
ã¡ã½ãããå»æ¢ããã¾ãããããã¯!!
ã¨åãæä½ãè¡ãã¡ã½ããã§ããã
// ã¿ãã«ããªããªã£ã¦ããã¼ã¿ã¯ã©ã¹ã§ä»£ç¨ã§ãã val pair: Pair<String, Int> = "hoge" to 123 val (str, n) = pair str //=> "hoge"
å°ãèéã«é¸ããã¨ããã®ææã«éå¿çãªWebãã¬ã¼ã ã¯ã¼ã¯ãç»å ´ãã¾ããã ååãKaraã¨è¨ããHTMLãCSSã¾ã§ãKotlinå é¨DSLã§åå®å ¨ã«æ¸ããã¨ããMVCãã¬ã¼ã ã¯ã¼ã¯ã§ãã ãããé¢ç½ããã¬ã¼ã ã¯ã¼ã¯ã§ãããããã¾ãããã¸ã§ã¯ãé²æãã¢ã¯ãã£ãã§ãªãã£ãã®ã§æµè¡ã¯ãã¦ããªãã¨æãã¾ãã
M4ã§ã¯ãã¼ã¿ã¯ã©ã¹ã«copy
ã¡ã½ããã追å ããã¤ãã¥ã¼ã¿ãã«ã¹ã¿ã¤ã«ã®å¤æ´ã容æã«ãªãã¾ããã
ãã¨ããã®ææã§ãããKAnnotatorãåºãã®ã¯ãããã¯æ¢åã®Javaã³ã¼ãã«NotNull/Nullableã®å¤é¨ã¢ããã¼ã·ã§ã³ãèªåä»å ãããã¼ã«ã§ãã
ã¨ãã³ã¸ã§ãªã¹ãçãªæ´»åã¨ãã¦ã¯ããã®ææãããã«æ¥çµã½ããã¦ã¨ã¢ã«åçºè¨äºãå¯ç¨¿ããã¦ããã ãã¾ããã
M5.xããã¤ã«ã¹ãã¼ã³ãªã®ã«å»ãã§æ¥ãã®ããã¨ã¡ãã£ã¨é©ãã¾ããã å é¨ã¯ã©ã¹ãã³ã³ã¹ãã©ã¯ã¿å¼æ°ã®ããã©ã«ãå¤ãã¬ã³ã¸ã®æ¹è¯ãé¢æ°ã®ãã©ã¡ã¼ã¿å¤æ°ã®å¤æ´ä¸å¯ãé¢æ°åç §ãªã©ããµãã¼ãããã¾ããã ã¾ããM5.2ã§ã¯SAMï¼Single Abstract Methodï¼å¤æã®ç¬¬ä¸æ©ãå®è£ ããã¾ãããM5.3ã§ã¯ãã使ãåæãè¯ããªã£ã¦ãã¾ãã
// M5.2 button.setOnClickListener(View.OnClickListener { ... }) // M5.3 button.setOnClickListener { ... }
ãããã大ããªã¨ããã¨ãã¦ã¯Delegated Propertyãå®è£ ããã¾ããã ããã¯ã¦ãã¼ã¯ãªæ©è½ã§ãããããããã£ã®é 延åæåã観測ãªã©è¨èªçµã¿è¾¼ã¿ã§ã¯ãªãAPIã¨ãã¦å®è£ ã§ãããã¨ãæå³ãã¾ãã
ãã®é ã«æ¥æ¬Kotlinã¦ã¼ã¶ã°ã«ã¼ããçºè¶³ãæ¥æ¬åã®Kotlinåå¼·ä¼ãéå¬ãã¾ããã
ãM11: Androidãæèãæé·ãç¶ãã
M6.xãSAMå¤æã®æ¹åãJava 8対å¿ã ã¢ããã¼ã·ã§ã³æ¹è¯ãJavaç¸äºéç¨æ§ã®åä¸ï¼static fieldï¼ãã©ã ãå¼ããå¤å´ã®é¢æ°ã®returnãTCOï¼Tail Call Optimizationï¼ã®ãµãã¼ããªã©ã
M7ã§ã¯ãçºè¡¨å½åããä»æ§ã«çãè¾¼ã¾ãã¦ããã¤ã³ã©ã¤ã³é¢æ°ãã¤ãã«å®è£
ããã¾ããã
é«éé¢æ°ã¸ã©ã ãå¼ã渡ãæã«ããªãã¸ã§ã¯ãåããã¤ã³ã©ã¤ã³å±éãããã¨ã§ã³ã¹ãã軽æ¸ããããã®æ©è½ã§ãã
ã¾ããä¾å¤ã®ãã§ãã¯ç¾©åããªãKotlinã³ã¼ããJavaãã使ãæ±ããããããããã«throws
ã¢ããã¼ã·ã§ã³ãå°å
¥ããã¾ããã
ãããããã§ç¬¬1åé¢è¥¿Kotlinåå¼·ä¼ãããã¾ããã
M8ã§ã¯ã¡ãã£ã¨ããå¤æ´ã
M9ã§ã¯M3ã§ãµãã¼ãããããã¼ã«ã«object
ãå»æ¢ããã¾ããã
ããã«platformStatic
ãplatformName
ã¨ãã£ãã¢ããã¼ã·ã§ã³ã§ããã«Javaã«æ©ã¿å¯ãä»çµã¿ãæä¾ããã¾ããï¼ããããä»ã§ã¯JvmStatic
ã¨JvmName
ã«ãªãã¼ã ï¼ã
ãã¨Platform Typeã¨å¼ã°ãããJavaã³ã¼ããKotlinã§è¦ãæã®NotNull/Nullableé¢ä¿ã®ãã¤ã
Kotlinç¨Androidãã¥ã¼ãã¤ã³ãã£ã³ã°ã©ã¤ãã©ãªã®KotterKnifeãå ¬éãããã®ã¯ãã®ãããã§ãã
M10ã§ã¯Reified Type Parameterãå®è£ ããã¾ããã ããã¯ã¸ã§ããªãã¯é¢æ°ã§åãã©ã¡ã¼ã¿ã®åæ å ±ãå®è¡æã«åç §ã§ããæ©è½ã§ãã
ãã®ææã2015å¹´æ¥ã«Software Designã§Kotlinå ¥éã®é£è¼ãã¹ã¿ã¼ããã¾ããã 第1å DroidKaigiã§Kotlinã«ã¤ãã¦çºè¡¨ãã¾ããã Kotlinã®ååãããããç¥ãã¦ããã¾ã«ããºãè¨äºãç»å ´ããããã«ãªã£ãã®ããã®ææã®åå¾ã§ãã
M11ã§ã¯ãM3ã§å»æ¢ãããã»ã«ã³ããªã³ã³ã¹ãã©ã¯ã¿ãããæ´ç·´ããããã®ãå°å
¥ãã¾ãä¸æ©Javaã«æ©ã¿å¯ãã¾ããã
ã»ãã«ã¯ãã¤ãã·ã£ã©ã¤ã¶ã«init
ãã¼ã¯ã¼ãå¿
é ãcompanion object
ãã©ã ãå¼ã®ããä¸ã¤ã®è¨æ³ãªã©ã®å¤æ´ãå
¥ãã¾ããã
Kotlinå é¨DSLã§Androidã«ãããã¬ã¤ã¢ã¦ããæ§ç¯ããAnkoã¨ããã©ã¤ãã©ãªãçºè¡¨ããã¾ããã ã¾ããKotlinã§Annotation Processingãè¡ãkaptã¨å¼ã°ãããã¼ã«ããªãªã¼ã¹ããã¾ããã
ãBeta2: Javaã¨ã®ç¸äºéç¨æ§100%ã¸ã®ææ¦
å½åããJavaã¨ã®ç¸äºéç¨æ§100%ã謳ã£ã¦ãã¾ããããå®éã«ã¯ç´°ããã¨ããã§ãã¾ãé£æºã§ããªããªã©ã®åé¡ãããã¾ããã ããã¾ã§ãJavaã¨Kotlinã仲è¯ãããããã®ä»çµã¿ãå°å ¥ããã¦æ¥ã¾ãããããã®é ããããä¸å±¤Javaãæèããããã«ãªã£ãã¨æãã¾ãã
M12ã«ã¯ãã¢ããã¼ã·ã§ã³ã®è¨æ³ã®å¤æ´ãããã«ä¼´ãã©ãã«ã®è¨æ³ã®å¤æ´ãtrait
ããinterface
ã¸ã®ãªãã¼ã ãåæåã®ææ³å¤æ´ãªã©ãå«ã¾ãã¾ãã
ã¤ã³ã¿ãã§ã¼ã¹ã®ã¯ã©ã¹ç¶æ¿ãéæ¨å¥¨ã«ãªãï¼ããã¯ã¡ãã£ã¨ã·ã§ãã¯ï¼ãclass object
ã¯å»æ¢ããã¾ããã
M12ãã3ã¶æ以ä¸ç©ºãã¦M13ããªãªã¼ã¹ããã¾ããã
ããããã£ã®åæåããã¨ã§è¡ããã¨ãæ示ããããã®ã¢ããã¼ã·ã§ã³lateinit
ãå°å
¥ãããDIãã¬ã¼ã ã¯ã¼ã¯ã¨ã®ç¸æ§ãè¯ããªãã¾ããã
ç´ååã表ç¾ããããã«Sealed Classããµãã¼ãããã¾ããã
ãã®ã»ãç´°ããå¤æ´ã¨ãJavaã¨ã®ç¸äºéç¨æ§ãé«ããå¤æ´ãããã¤ãå
¥ãã¾ããã
æ¥æ¬èªã§Kotlinã«ã¤ãã¦è©±ããSlackãã¤ããã¾ããã
M14ã§ã¯operator
ãconst
ãªã©ã®ãã¼ã¯ã¼ãã追å ããã¾ããã
ãããJavaã¨ä»²è¯ããã工夫ã§ãã
ããã¦ä»å¹´10æã«1.0-Betaåè£çãããã®2é±éå¾ã«1.0-Betaããªãªã¼ã¹ããã¾ããï¼
infix
ãã¼ã¯ã¼ããå°å
¥ãããªã©ãJavaã¨ã®è¦ªåæ§ãããã«é«ããå¤æ´ãå
¥ãã¾ããã
11æ16æ¥ã«ã¯1.0-Beta2ããªãªã¼ã¹ãããå°ããªå¤æ´ãå
¥ãã¾ããã
ããã¦1.0ã¸
ãããã1.0ããªãªã¼ã¹ããã...ã¯ãã§ãã 3å¹´å以ä¸Kotlinã®æé·ãè¦ã¦ããåãéä¸ã§é æ«ããããã¸ã§ã¯ããå°ãªããªãä¸ãæ£å¼ãªãªã¼ã¹ã«ããçããã®ã¯æ¬å½ã«å¬ããã§ãã
Kotlinã¦ã¼ã¶ãå¢ãã¦ãã¾ããã ã¨ã³ã¸ãã¢ã®æ±äººãã¼ã¸ã«ã使ç¨è¨èª: Kotlinãã¨è¨è¼ãã¦ããä¼æ¥ãã¡ãã£ã¨ãã¤å¢ãã¦ãã¾ããï¼ãããï¼ï¼ã ä»å¹´ã®Kotlinã¢ããã³ãã«ã¬ã³ãã¼ã¯ããã«æºå¡ã«ãªãã¾ããï¼
å¬ãããªã¼ã3å¹´åã¨ãã¯ãã¨ãããããï¼ã£ã¦è¨ã£ã¦ãï¾ï¾ï½°ï½¸ï½½ï½¸ï½½ã£ã¦ãªãã®ããªãã ã£ã
— ããã (@ngsw_taro) November 25, 2015
ã¨ã¯ãããæªå®è£ ã®æ©è½ãããã®ãæ°æããã§ãã ä¾ãã°åã¨ã¤ãªã¢ã¹ï¼ãããããçµå±ããããã ã£ãï¼ï¼ãªã©ã ã¨ããã1.0ã®ãªãªã¼ã¹ã¯æ¥½ãã¿ã§ããããããã£ããã«å°å ¥ããä¼æ¥ãå¢ããããããªãã
夢
JetBrainså ¬èªã¨ãã³ã¸ã§ãªã¹ãã«ãªããã¨ã ä¸å¿ä»ã¯ç§ãåæã«ãã£ã¦ãæ´»åãªã®ã§ãèªç§°ããå¿ ãä»ãã¦èª¤è§£ãåé¿ãã¦ãã¾ãã ééçãªå ±é ¬ã¯ãããªãã®ã§ãå ¬å¼ã«èªãã¦æ¬²ããã§ãï¼å°æ¹é å¾ã®æ è²»ãªã©ã®çµè²»ã¯ããããããã©ã...ï¼ã
ãã1ã¤ã®å¤¢ã¯ãKotlinã巨大ããã¸ã§ã¯ãã§æ¡ç¨ããããã¨ã Kotlinã¯Javaãããåå®å ¨ã ããNULLå®å ¨ã§ãã ææ³ãã·ã³ãã«ã§ãããããã¨ãç´ ç´ã«æ¸ãä¸ããç練度ã®å·®ã«ããã³ã¼ãã®ã°ãã¤ããæããã大人æ°ã®ããã¸ã§ã¯ãã«åãã¦ããã¨æãã¾ãï¼ãã ã®ä¸»è¦³ï¼ã ã¨ã³ã¸ãã¢ã®QOLã®åä¸ã«ã¤ãªããã°ãã¨ãã³ã¸ã§ãªã¹ãå¥å©ã«å°½ãã¾ãã
ãããã«
ã¨ããæãã®ã ãã ãããã¨ã³ããªã§å§ã¾ã£ãKotlin Advent Calendar 2015ãªããã§ãããææ¥ä»¥éã®çãä¸ããã«æå¾ ã§ããï¼ï¼ ææ¥ã¯@takuji31ãããdelegateé¢ä¿ã«ã¤ãã¦æ¸ãã¦ããã ããããã§ãï¼ ããã§ã¯ããããã¨ããããã¾ããã
*1:åæ§æãæ¹åãé²ã¿ãä»ããã®ã³ã¼ãã¯æ®ã£ã¦ãã¾ãããã