æ°ããè¨èªãè¦ããããã«ç§ãããäºï¼Kotlinã®å ´åï¼
å æ¥ã®ãScalaåå¼·ä¼ç¬¬170å in æ¬é· : ãµããã¼ããScalaã®è¨èªä»æ§ã
ã§Scalaã®è¨èªä»æ§ã«ã¤ãã¦è§£èª¬ãã¦ããã¨ãã®åå¿ãã¿ã¦ãã©ãããèªåã®ããã°ã©ãã³ã°è¨èªã®ææ¡ã®ä»æ¹ã¯ãã¾ãä¸è¬çã§ã¯ãªãã®ã§ã¯ã¨ãããã¨ãèãã¾ãããã©ãéããã¨ããã¨ä¸è¨ã§ã¯èª¬æã§ããªãã®ã§ãããä¸éçã«ã¯ãããã°ã©ãã³ã°è¨èªã«ã¤ãã¦ã¯ããããã£ã¼ãªã³ã°çã«ãªãã¨ãªãç解ãã¦ããé¨åç解ãã¦ããªãé¨åãã¼ãã¼ã£ã¨ãã¦ããã®ã«å¯¾ãã¦ãèªåã®å ´åãç解ãã¦ããé¨åã¨ããã§ãªãé¨åã®å¢çããã£ãããã¦ãããããªæãã§ãã
ããã¯ã¨ãããã¨ãã¦ããã®ã¨ã³ããªã§ã¯ãèªåãæè¿æ°ãã触ã£ãè¨èªã§ããKotlinã«ã¤ãã¦ãã©ã®ããã«ãã¦ç解ãé²ããããæ¸ãã¦ã¿ããã¨æãã¾ãã
å ¬å¼ããã¥ã¡ã³ããèªã
å®çªã¨ããã°å®çªã§ãããå ¬å¼ããã¥ã¡ã³ããä¸çªæ£ç¢ºã«è¨èªã«ã¤ãã¦æ¸ãã¦ãããã®ã§ããKotlinã®å ¬å¼ãªãã¡ã¬ã³ã¹ã¯ä»¥ä¸ããè¦ããã¨ãã§ãã¾ãã
ä»ã¾ã§è²ã ãªè¨èªã触ã£ã¦ãã¾ãããããã®ä¸ã§ãKotlinã®ãªãã¡ã¬ã³ã¹ã¯ããæ¸ããã¦ããæ¹ã ã¨æãã¾ããã§ãããããã§ã¯ãããããã¨ã¯å²ã¨ã©ãã§ãããã¦ãã¨ã«ãããµã³ãã«ã³ã¼ãããã£ã¨çºãã¦ãèªåã®è³å ã«ãããã£ã±ã«ææ³å®ç¾©ã®ãããªãã®ãæ§ç¯ãã¾ãããã®æç¹ã§ã¯BNFã»ã©ãã£ããã¨ããå½¢ã§ã¯ãªãã§ããã
- ï¼ã¯ã©ã¹ï½ãªãã¸ã§ã¯ãï¼ã¯ä»»æåã®ã¡ã³ãã¼å®ç¾©ãããªã£ã¦ãã
- ã¡ã³ãã¼ã®ååã¯çç¥å¯è½
- ã¡ã½ãããåä¸å¼ãããªãå ´åã`='ã«ç¶ãã¦æ¬ä½ãè¨è¿°ããï¼æ³¨ï¼ä¾¿å®ä¸ã¡ã½ããã¨å¼ãã§ãã¾ãï¼
- ã¡ã½ãããè¤æ°ã®å¼ãå«ããããã¯ãããªãå ´åã'='ã§ã¯ãªããç´å¾ã«
{ ... }
ãç¶ãã
ã¨ãã£ãäºå®ãç©ã¿ä¸ãã¦ãå ¨ä½ã¨ãã¦ã©ã®ãããªææ³ã«ãªã£ã¦ããããæ¨æ¸¬ãã¦ããã¾ããææ³ã«ã¤ãã¦çåã湧ããã¨ãã¯ããããã ãããã¨ããããã¾ããªä»®èª¬ãç«ã¦ã¦ããããæ£ããããæ¤è¨¼ãã¾ããè¨èªã®ãªãã¡ã¬ã³ã¹ã¯ææ³ã«ã¤ãã¦ç¶²ç¾ çã«è§£èª¬ãã¦ãã¾ãããããã³ã³ãã¤ã©ã«èãã®ãæã£åãæ©ãã§ããããã§ãã³ã¼ãã¼ã±ã¼ã¹ãå«ãå ¥åãã³ã³ãã¤ã©ã«é£ããã¦ãã¼ãºã¨ã©ã¼ã«ãªããã©ããçã調ã¹ã¾ãã
ãã¼ã¶ã³ã³ããã¼ã¿ã©ã¤ãã©ãªãä½æ
ä¸è¦ãã¿ã£ã½ãã§ãããããã¯çµæ§é¦¬é¹¿ã«ã§ããªãå¹æãããã¨æãã¾ããHello, World!ããå¦ã¹ããã¨ã¯æ¥µãã¦éããã¦ãã¾ããããã¼ã¶ã³ã³ããã¼ã¿ã©ã¤ãã©ãªãä½ãã¨
- ã¸ã§ããªãã¯ã¹ãå«ãåã·ã¹ãã
- æååã®åºæ¬æä½
- é¢æ°ã®åãæ±ãï¼é¢æ°ã第ä¸ç´ãªãã¸ã§ã¯ãã¨ãã¦æ±ãå ´åãå«ãï¼
- ã¯ã©ã¹ããªãã¸ã§ã¯ãã®åãæ±ã
- etc
ãªã©æ§ã ãªãã¨ãä¸åº¦ã«å¦ã¹ã¾ãã
å ¬å¼ããã¥ã¡ã³ãã®çåç¹ãçªãè©°ãã
ããã¯ãå ¬å¼ããã¥ã¡ã³ãã®èª¬æãä¸è¶³ãã¦ããã¨æããå ´åã«ããè¡ãæ¹æ³ã§ãããã¨ãã°ãKotlinã§ã¯ãsmart castããããã¨ããã®ã謳ãæå¥ã«ãã¦ã¾ãããã©ã®ãããsmartãªã®ãã«ã¤ãã¦èª¬æãããã¾ããã
val s: String? = ... if(s != null) { println(s.length) }
ã¨ãã£ããµã³ãã«ãè¦ãããã¦ãããããããã¨ãã°
if(false || s != null) { println(s.length) }
ã¯OKãªã®ãã¨ãã
if(!!!!!!(s != null)) { println(s.length) }
ã¯OKãªã®ãã¨ããè²ã çåã湧ãã¦ãã¾ããããã§éè¦ãªã®ã¯ãå®ç§ãªã¹ãã¼ããã£ã¹ãã¨ãã£ããã®ã¯åºæ¬çã«ä¸å¯è½ãªã®ã§ãåã·ã¹ãã ã«ããã¦ä¸è¬çãªããå®å ¨å´ã«åããï¼ï¼not-nullableã¨å¤å®ãããå ´åã¯å¿ ãnot-nullableã ããå ¨ã¦ã®not-nullableãæ¤ç¥ã§ããããã§ã¯ãªãï¼ææ³ãåã£ã¦ããã«éããªãã¨ããæ¨æ¸¬ãåããããã¨ã§ããããã¦ããã©ã®ç¨åº¦ã®è¿ä¼¼ç²¾åº¦ãªã®ããã確ãããããã«ãè²ã ãªããã°ã©ã ãé£ããã¦ã¿ã¾ãã
ã¯ãããã£ãäºã調ã¹ãéç¨ã§ããã£ããã¨ãæ¸ãçãããã®ã§ãã
ã¾ããKotlinã®å
¬å¼ããã¥ã¡ã³ãã«ããã°ããããã¯ãããªãé¢æ°å®ç¾©ã¯ãå¿
ãreturn
ãæ¸ããªããã°ãããªãã¦ããã®æ ¹æ ã¨ãã¦
Kotlin does not infer return types for functions with block bodies because such functions may have complex control flow in the body, and the return type will be non-obvious to the reader (and sometimes even for the compiler).
ã¨ããã®ãæ¸ããã¦ãã¾ãããã©ã ãå¼ã¯è¤æ°ã®å¼ãæã¦ãã®ã«returnãæ¸ããªãã¦è¯ããã¨ãèããã¨ããã®ããã¥ã¡ã³ãã®è¨è¿°ã¯ããããã§ãããããç«è¨¼ããããã«ã次ã®ãããªblock
é¢æ°ãå®ç¾©ããå®éã«ãè¤æ°ã®å¼ãããªãé¢æ°å®ç¾©ã«ããã¦return
ãä¸è¦ã«ã§ãããã¨ã確èªãã¾ããã
inline fun <T> block(body: () -> T): T { return body() }
ãã®è¾ºã§æèãã¦ããã®ã¯ãå ¬å¼ããã¥ã¡ã³ãã®ãµã³ãã«ã³ã¼ãã¯ä¿¡ç¨ãããã説æã«ã¤ãã¦ã¯è©±ååãããã«æã£ã¦ããã¨ããæ 度ã§ããè¨èªä»æ§æ¸ã§ããã°æ£ç¢ºãæãã¦ãããã¨ãæå¾ ã§ãã¾ãããè¨èªã®å ¬å¼ããã¥ã¡ã³ãã¯ãé åçãªæ©è½ãæ°è¦ã¦ã¼ã¶ã«å¯¾ãã¦ã¢ãã¼ã«ããå ´ã§ãããããã°ãã°æ£ç¢ºãã¯ç ç²ã«ãªãã¾ãã
ããã«ãµã³ãã«ããã°ã©ã ãæ¸ã
ãã®æç¹ã§ãè¨èªã®æ§æãåã·ã¹ãã ã«ã¤ãã¦ããç¨åº¦ææ¡ã§ãã¦ãã¾ããããããæ·±ãç解ãããã®ã§ãããã«ããã¤ãã®ãµã³ãã«ã³ã¼ããæ¸ãã¦å®é¨ãã¦ã¿ã¾ãããã§ããã ããè¨èªã®ã·ã¹ãã ã®ã³ã¼ãã¼ã±ã¼ã¹ãä»ããããªããããããµã³ãã«ã³ã¼ããèãããããéãã¦ããã«ç解ãæ·±ãã¾ãã
ã½ã¼ã¹ãèªã
ã ãããã®è¨èªå¦çç³»ã®ã½ã¼ã¹ã«ã¯BNFã«ããææ³å®ç¾©ãã¤ãã¦ããã®ã§ããªãã ãããããããªãæ§æãç»å ´ããã¨ãã¯ãããèªãã°ã ããã解決ã§ãã¾ããããã§ãããããªãå ´åãå®éã«åå¥è§£æå¨ãæ§æ解æå¨ã®ã½ã¼ã¹ã¾ã§èªã¿ã¾ããä»åã®å ´åãåå¥è§£æå¨ã®ã½ã¼ã¹ã¯èªãã ãã®ã®ãæ§æ解æå¨ã¯é¢åããã ã£ãã®ã§ããã¦èªã¿ã¾ããã§ãããããã®æ°ã«ãªãã°èªããã¨æãã¾ãã
åãã§ãã«ã¯ããã¼è§£æãè¡ãé½åä¸ãé常ã®éçåä»ãè¨èªããããããããã ãªã¨æã£ãã®ã§ãä»ã®ã¨ããã½ã¼ã¹ã¯èªãã§ãã¾ãããæ£ç´ãKotlinã®ããã¼éä¾åã®é¨åã®åã·ã¹ãã ã¯å¤§ä½ããã£ãã¨æãã®ã§ãããããã¼ä¾åã®é¨åã«ã¤ãã¦ã¯ã¡ããã¨ç解ãã¦ãã¾ãããã§ããããããã¼éä¾åã®é¨åã«ã¤ãã¦ã¯ç解ã§ããããããã¼ä¾åã®é¨åã«ã¤ãã¦ã¯æªç解ã®é¨åãå¤ããã¨ãã£ãå½¢ã§ãç解ãã¦ãããã¨ã¨ããã§ãªããã¨ã®éã«å¢çç·ãå¼ãã¦ããããããã°è¯ã話ã§ãã
ã¾ã¨ã
ã ãã ãã¨æ¸ãã¦ãã¾ãããã
- ã³ã³ãã¤ã©ä½è ã®æ°æã¡ã«ãªã£ã¦è¨èªä»æ§ãæ¨æ¸¬ãã
- ææ³ã®å ¨ä½åãæãæããªãããéæä¿®æ£ãã
- ç解ãã¦ãããã¨ã¨ç解ãã¦ããªããã¨ã®éã«é©åãªå¢çç·ãå¼ã
辺ããèªåã«ã¨ã£ã¦ã®éè¦ãªãã¤ã³ããªã®ã§ã¯ãªããã¨æãã¾ãããç¹ã«ãæå¾ã®ç¹ã¯å人çã«ä¸çªéè¦ãªç¹ã§ããç解ãã¦ããªããã¨ã®ç¯å²ãé©åã«ããã£ã¦ããã°ï¼ãããã¯ç解ãã¦ããªãç¯å²ããããåºããã°ï¼ãããã°ã©ãã³ã°è¨èªã®å ¨ä½åã«ã¤ãã¦ããã£ã¦ããªããã¨ãæããå¿ è¦ã¯ãªãããã§ãã
ãªããå½ããåã§ãããKotlinã«ã¤ãã¦ããç解ããããã¨ãã£ã¦ãããã§Kotlinã§å®ç¨çãªããã°ã©ã ãæ¸ããããã«ãªãããã§ã¯ããã¾ããã