Kotlinã¨Scalaã®éã (é¨åçãªç¿»è¨³ï¼ Kotlin vs Scala: Which Problems Do They Solve?)
ã¯ããã«
KotlinãScalaã£ã¦ä½ãéããã ããï¼ã£ã¦æã£ã¦ããããæ¢ãã¦ã¿ãã¨ãããè±èªã®è¨äºããã£ã¦ããããããã£ãã®ã§ãå½¹ã«ç«ã¡ãããªé¨åã ãé¨åçã«ç¿»è¨³ãã¦ã¿ã¾ããã1èª1èªè¨³ãã¦ããã¨å¤§å¤ã ãããã£ããç¥ããã人ã«ã¯å¿ è¦ãªãããªã¨æãã®ã§ããã£ããçãã¦æ¥æ¬èªã«ãã¦ã¾ãã
æéããªã人ã¯ããã®è¨äºã®æå¾ã®æ¹ã®ãã¾ã¨ããé¨åã ããèªãã ãè¯ãã¨æãã¾ãã
ééããªã©ãã£ããããããªãããééããææãã¦ãã ããã¨å©ããã¾ãã»ã»ã»ã
ããã¦ããã²åæãåèã«ãã¦ãã ãããÂ
å è¨äº
https://superkotlin.com/kotlin-vs-scala/
Kotlinã¨Scalaã®éã
è¿å¹´ãScalaã¨KotlinãJavaã«å¯¾æãã¦äººæ°ãããã¦ãã¾ããå°ãªãã¨ãJVMã使ããã¨ããã¨ãã«ã¯ãScalaã¨Kotlinã使ããã¨ãè¨ãããã¨æãã¾ãã
Scalaã¨Kotlinã¯ãããè¯ãJavaãæä¾ãããã¨ããåãç®çã®ãã¨ä½ããã¦ãã¾ãããç°ãªãã®ãç®çã¸ã®ã¢ããã¼ãæ¹æ³ã§ãã
ã¾ããScalaã¯å¦è ãã¡ã«ãã£ã¦çã¿åºãããKotlinã¯ã½ããã¦ã§ã¢ä¼ç¤¾ã§ä½ããã¦ãã¾ããScalaã¯é¢æ°åè¨èªãç°ãªãããã°ã©ãã³ã°ãã©ãã¤ã ãï¼ã¤ã®è¨èªã«å ¥ãã¦ãä½ãããããã®ãä½ã£ã¦ã¿ããã¨ãã姿å¢ã§ä½ããã¦ãã¾ããä¸æ¹ãKotlinã¯ãã³ã³ãã¤ã«æéãçãããããã¼ã«ã®å å®ãªã©ãå³ããªã©ãã½ããã¦ã§ã¢éçºã®ç¾å ´ã§èµ·ããç¾å®çãªåé¡ã解決ããããã«ä½ããã¦ãã¾ãã
Scalaã®ãããã¨ããã¯ãJVMä¸ã§åããæ°ããéçè¨èªãä½ããã¨è¨¼æããã¨ããã«ããã¾ãããã®å¾ãããªãã®æéãçµã£ã¦ããKotlinãåºç¾ãããã®é ã«ã¯JVMä¸ã§èµ°ãæ°è¨èªãèãããã¨ããããå®ç¨çã«ä½¿ãããã¨ãéè¦è¦ããããã«ãªã£ã¦ãã¾ããã
以ä¸ã§ã¯ãKotlinã¨Scalaãæ¯è¼ãã¦ããã¾ãããæè¡çãªæ©è½ã§ã¯ãªãã使ãæ¹ã®é¢ã§æ¯è¼ããã¦ããã¾ããã¾ãã2ã¤ã®è¨èªã®æ©è½ã®æ¦è¦ãè¦ã¾ãããã®å¾ãä½ã®ããã«ä½ãããè¨èªãªã®ããã«ç¦ç¹ããã¦ã¦è¦ã¦ããã¾ãã
Javaã®ä½ãæªãï¼
KotlinãScalaãJVMä¸ã§èµ°ããããJavaã¨ããæ¯è¼ããã¾ããã§ã¯ãJavaã«ã¯ãªã«ãæªãã¨ãããããã®ã§ããããï¼
Javaã¯ãä¸åº¦æ¸ãããã©ãã§ã使ãããã¨ãã売ãæå¥ã§ãã¾ãããéã«ä¸ã«åºããã¾ããã
ãããããä¸çã§ä¸çªä½¿ããã¦ããããã°ã©ãã³ã°è¨èªã¯Javaã§ããããä¸çä¸ã®ä¼æ¥ã§ä½¿ããã大å¦ã®ææ¥ã§ãåºã使ããã¦ãã¾ãã
Cããæé·ãC++ããããããªè¨èªã§ããJavaã§ãããæè¿ã§ã¯ä»ã®è¨èªã«é ããã¨ã£ã¦ãã¾ããC#ã¨æ¯ã¹ã¦å£ãã¨è¨ã人ãå¤ããã¾ãã
ããã£ã¨Javaãçç£æ§ã®é«ãè¨èªã ã£ããããã®ã«ãããã£ã¨æ¥½ã«æ¥½ãã使ããè¨èªã ã£ããããã®ã«ãã¨æãä¼æ¥ãããã°ã©ãã¼ã¯å¤ããã¨ã§ããããã¾ããJavaã®è¨èªä»æ§ä¸ã®å¶ç´ãããããã®å¶ç´ã®ããã«æ¹å¤ãããã®ã確ãã§ããããããã ããã¨è¨ã£ã¦ãããç°¡åã«Javaãå¤ãããã¨ã¯ã§ãã¾ããããã§ã«ä¸çä¸ã§åºã使ããã¦ããããã«ãæªãé¨åããã£ã¦ãæ ¹å¹¹é¨åãå¤ãããã¨ãã§ããªãã®ã§ããï¼ãããªãJavaãã®ãã®ãå¤ãããããã¾ã¾ã§ä½ã£ããã®ãåããªããªã£ã¦ãã¾ãã¾ããããã»ã»ã»ãï¼
Kotlinã¨Scalaã®ä¸»ãªéã
ãªãã¸ã§ã¯ãæå
Javaã¯ãªãã¸ã§ã¯ãæåã®è¨èªã§ãããJavaå ã®ãã¹ã¦ã®ãã®ããªãã¸ã§ã¯ãã¨ããããã§ã¯ããã¾ãããä¾ãã°ãintã¯ããªããã£ãåã§ããScalaã¨Kotlinã§ã¯ãã¹ã¦ããªãã¸ã§ã¯ãã§ããããªãã¸ã§ã¯ãã¨ãã¦æ±ãã¾ãã
ã¾ããScalaã¨Kotlinã¯ä»ã®ããã°ã©ãã³ã°ãã©ãã¤ã ãåãè¾¼ãã§ãã¾ããKotlinã¯ä¸»ã«ãªãã¸ã§ã¯ãæèã§ãããæ®æ®µã®ããã°ã©ãã³ã°ã§ä¾¿å©ã§ãããæ©è½ãä»ã®ããã°ã©ãã³ã°ãã©ãã¤ã ããåãè¾¼ãã§ãã¾ãã
é¢æ°åããã°ã©ãã³ã°
 Kotlinã¯é¢æ°åããã°ã©ãã³ã°ã®ãã¹ã¦ã®æ©è½ããµãã¼ããã¦ããããã§ã¯ããã¾ããããã©ã ãå¼ãé«éé¢æ°ãªã©ã®é¢æ°åããã°ã©ãã³ã°ã®æ©è½ããµãã¼ããã¦ãã¾ãããã¨ãã°ã以ä¸ã®æ©è½ããµãã¼ããã¦ãã¾ãã
- ã¦ã¼ãã£ãªãã£ã¼ã¯ã©ã¹ãä½ã£ã¦ã¡ã½ãããã¾ã¨ããªãã¦ããé¢æ°ã ããä½ãã ãã§OKã
- Kotlinã¯statementsã§ã¯ãªãexpressionsãå®ç¾©ã§ããã®ã§ãifã®çµæãªã©ãå¤æ°ã«æ ¼ç´ãããã¨ãã§ãã¾ãã
-  var ã§ã¯ãªã val ã使ãã°ã¤ãã¥ã¼ã¿ãã«ãªãã¼ã¿ãä½ãã¾ããfinal ãªã©ã®ä¿®é£¾åã¯å¿ è¦ããã¾ããã
Scalaã¯é¢æ°åããã°ã©ãã³ã°ãã©ãã¤ã ã¨ãªãã¸ã§ã¯ãæåããã°ã©ãã³ã°ãã©ãã¤ã ã®ä¸¡æ¹ããµãã¼ããã¦ãã¾ããScalaãä½ã£ãç®çã¯ã¾ãã«ããã§ãããHaskellã§è¦ãããã«ãªã¼åãã¡ã¢åãPartial applicationãtype classesãªã©ã®ã»ã¨ãã©ã®æ©è½ãæã£ã¦ãã¾ãããã®ãããªæ©è½ã¯KotlinãJavaã«ã¯ããã¾ããã
Javaã¨ã®ç¸äºéç¨æ§
Kotlinã¯Javaã§åããã¨ãèãããã¦è¨è¨ãããè¨èª
Kotlinã¯Javaã¨ã®ç¸äºéç¨æ§ãããããã«ä½ãããè¨èªã§ããããã«è¨ãã°ãJavaã¨ã®ç¸äºéç¨æ§ãé«ããããã®æ©è½ãæå³çã«å®è£ ããã¦ãã¾ãããã¨ãã°ãJavaããKotlinã®ã³ã¼ããç°¡åã«å¼ã³åºããä»çµã¿ãåãã£ã¦ãã¾ããã§ãã®ã§ãã¬ã¬ã·ã¼ãªã¢ããªã±ã¼ã·ã§ã³ã¯Javaã§éçºããæ°è¦éçºã¯Kotlinã§è¡ããã¨ãããã¨ãå¯è½ã§ãã
Androidã§Kotlinãããã ã人æ°ãããã®ã¯ãKotlinãJava 6ï¼ç¾å¨ã®Androidã®Javaãã¼ã¸ã§ã³ï¼ã¨ç¸äºéç¨æ§ãããããã§ãããã
Javaã¨ã®ç¸äºéç¨æ§ãé«ãã®ã¯å¶ç¶ã§ã¯ãªããããããJetBrains社ãKotlinãè¨è¨ããã¨ãã«å½æã®Javaã®ç¶æ³ãèæ ®ãã¦è¨è¨ãã¦ããããã§ããã©ããããKotlinããã£ã¨ä½¿ããããã§ããããèãã¦è¨è¨ããã¦ãã¾ãããã®çµæãããã¤ãã³ã¼ãã®æ§æã¨ããç¹ã«ã¾ã¨ãããã¾ããããã¦ããã®ç¹ããCordaãScalaã§ã¯ãªãKotlinãé¸ãã çç±ã®1ã¤ã«ãªã£ã¦ãã¾ãã
Scalaã¯Javaã§åãããã¨ã¯å¯è½
ããç°¡åã«ãããã¤ã§ããããã«ãScalaã¯JVMãã©ãããã©ã¼ã ã§åãããã«ä½ããã¾ãããããããªãããç¸äºéç¨æ§ã¯ä¸çªã®ç®çã§ã¯ããã¾ããã§ãããå°ããè¨èªã®æ©è½ã«é¢ããæè¡çãªè©±ã«ãªãã¾ãããä¾ãã°ãScalaãåããããã«ã¯Java 8ãå¿ è¦ã§ãã
ãããè¸ã¾ããã¨ããã§ã«ããJavaã³ã¼ããã¼ã¹ãScalaãã使ãããã¨ããã°ä½¿ããã¨ã¯ã§ãã¾ãããScalaã¨Javaãè¡ã£ããæ¥ããããªããã®éçºã¯é£ãããScalaã®æ³å®ãããã¦ã¼ã¹ã±ã¼ã¹ã§ã¯ããã¾ããã
é¢æ°åããã°ã©ãã³ã°ã¨ãªãã¸ã§ã¯ãæåããã°ã©ãã³ã°ããµãã¼ãããããã«ãScalaã§ã¯äºææ§ãç ç²ã«ããã¦ãã¾ãããããScalaã®é«åº¦ãªæ©è½ã使ããã¨ããã¨ãJavaããã¯Scalaã®é«åº¦ãªæ©è½ããã¾ã使ããªããªã£ã¦ãã¾ãã§ããããä»®ã«ã§ããã¨ãã¦ããJavaããã°ã©ãã¼ã¯ã©ã®ããã«ä½¿ã£ã¦ããã®ãã¯ããããªãã§ãããã
å²å¦ã®éã
ä»æ¥ãæåãåãã¦ããè¨èªã®å¤ãã¯ãæ確ãªç®çã¨ãã®è¨èªãã©ãããã¹ãããã¯ã£ããã¨ããå²å¦ãæã£ã¦ãã¾ããKotlinã¨Scalaãä¾å¤ã§ã¯ããã¾ãããKotlinã¯ããè¯ãJavaã«ãªããã¨ãç®æããScalaã¯Javaãè¶ ããè¨èªã«ãªããã¨ãã¦ãã¾ãã
Kotlinã¯ããè¯ãJava
Kotlinã«ã¤ãã¦ã¯ãå½åã®ç®çããã§ã«éæããã¨è¨ã£ã¦è¯ãããããã¾ããããªãã¨è¨ã£ã¦ããJavaã®ä»ã«Addroidã®éçºè¨èªã¨ãã¦æ£å¼ãµãã¼ããããæåã®è¨èªã¯Kotlinã§ããããããã¯ã¤ã¾ããJavaã³ãã¥ããã£ãKotlinãæ°ã«å ¥ã£ãã¨ãããã¨ãããã¦ãKotlinã¯Javaéçºè ã«ã¨ã£ã¦åãå ¥ããããã¨ãããã¨ãæå³ãã¾ãã
Kotlinã¯å®ç¨æ§ã®é«ãè¨èªã§ãããJavaéçºè ã«ã¨ã£ã¦ã親ãã¿ããããé«ãçç£æ§ãå®ç¾ã§ããè¨èªã§ããåºæ¬çã«ã¯ãJavaã®ãããªè¨èªã«ãããã¨ãã¦ãã¾ããããã®ä¸ã«C#ããããã«ã¯Scalaã®æ©è½ãåãè¾¼ãã§ãã¾ããJavaéçºè ã欲ããããããªã©ã ãå¼ãé¢æ°åããã°ã©ãã³ã°ã®åºæ¬çãªæ©è½ãããã¦ããã°ã©ãã³ã°ã楽ã«ãã¦ãããsmart castingãnon-nullable typeã®æ©è½ã追å ããã¦ãã¾ãããããã«ããããã¾ã¾ã§åé·ã ã£ãã³ã¼ããç°¡æ½ã«æ¸ããã¨ãã§ãã¾ãã
Kotlinã§ã¯é¢æ°åããã°ã©ãã³ã°ãããç¨åº¦ãµãã¼ãããã¦ãã¾ãããããã¯æç¶ãå/å½ä»¤åããã°ã©ãã³ã°ãããç°¡åã«ããããã ãã®ãã®ãããµãã¼ãããã¦ãã¾ããï¼ä¾ãã°ãåã ã®é¢æ°ã¯ç¬ç«ãã¦ç¬¬ä¸ç´ãªãã¸ã§ã¯ãã¨ãã¦æ±ãã¾ãï¼ãKotlinã¯ã¬ãã¬ããªé¢æ°åè¨èªã«ãªããã¨ãã¦ããã®ã§ã¯ãªããã·ã³ãã«ã§ãããã¨ãåªå ãã¦ãã¾ãã
Scalaã¯Javaãè¶ ãããã¨ãã¦ããè¨èª
Scalaã¯Kotlinã¨ã¯ã¾ã£ããå¥ã®ç®çãæã£ã¦ãã¾ããScalaã®ç®çã¯Javaããå¼·åãªè¨èªãJavaã§ã¯ã§ããªããã¨ãã§ããè¨èªããä½ããã¨ã§ããã
Scalaã§ã¯ããªãã¸ã§ã¯ãæåããã°ã©ãã³ã°ã«å ãã¦ãé«åº¦ãªé¢æ°åããã°ã©ãã³ã°ããµãã¼ãããã¦ãã¾ãããã®ããã«Scalaã¯è¤éã«ãªããå¦ãã ã使ã£ããããã®ãé£ããã¨ããè©å¤ãã¤ãããã«ãªãã¾ããããã¡ããããã¯è¨èªèªä½ã«åé¡ãããã®ã§ã¯ãªããåç´ã«å¤ãã®ãã¨ãã§ããè¨èªã§ããããã«å¤ãã®ãã¨ãå¦ã¶å¿ è¦ãããã®ã§ãã
ãã¡ããã®ãã¨ãæ£ãã使ãã°ã¡ãªããã¯å¤§ããã§ãããæ£ãã使ããªããã°ã¡ãªããã¯åºãªããªãã¾ããScalaã§ã¯è¤æ°ã®ããã°ã©ãã³ã°ã¹ã¿ã¤ã«ãåå¨ããã©ã®ã¹ã¿ã¤ã«ããã¹ããªã®ãã¯æ··ä¹±ã®ãã¨ã«ããªã£ã¦ãã¾ããããã«ãããéçºã³ã¹ãã¯é«ããªããScalaéçºè ã®åå ¥ãé«æ°´æºã§ãããããªãã®ç«å ´ãéçºè ãªã®ãããã¨ãéçºè ãéãä¼æ¥ãªã®ãã«ãã£ã¦ãããã¯è¯ãç¹ã«ãæªãç¹ã«ããªãã¾ãã
è¨èªãå²ãä¸ç
ã³ãã¥ããã£
Scalaã®æ¹ã大ããã³ãã¥ããã£ãæã£ã¦ãã¾ããè¨èªã®è¯ãæªãã¯å¥ã«ãã¦ãScalaã®æ¹ãé·ãåå¨ãã¦ããã®ã§ããããããããããããã¯å½ç¶ã®ãã¨ãªã®ããããã¾ãããå¯ä¸ããããå½ã¦ã¯ã¾ããªãã®ã¯Androidéçºã§ããããKotlinã®äººæ°ã®é«ãã¯ã2017 Google I/Oã§ã®Kotlinçºè¡¨ã®æ§åãããæããã§ãããã
Kotlinã®ãã¤ãã£ãã©ã¤ãã©ãªã¼ããã¾ãå å®ãã¦ããªããã¨ã¯ãJavaã¨ã®äºææ§ãåããã¨ã§ãããã軽æ¸ããã¦ããã§ããããããããå ç¢ãªKotlinã©ã¤ãã©ãªã¼ã¯ScalaãJavaã®ããã«ã¯ååéçºã¯ããã¦ãã¾ããã
ããã¥ã¡ã³ããå¦ç¿ãããã
ã©ã¡ãã®è¨èªãããã¥ã¡ã³ãã¯å å®ãã¦ãã¾ãï¼å¤ãã£ãScalaã¦ã§ããµã¤ããä»ã§ã¯ææ°ã«ã¢ãããã¼ãããã¦ãã¾ãï¼ãScalaã使ãéã«ã¯ãã¾ãéçºè ããã¬ã¼ãã³ã°ãã¦ãScalaã«æ £ãããã¦ããèªèº«ã®éçºã¹ã¿ã¤ã«ãå¦ç¿ãããå¿ è¦ãããã¾ããJavaéçºè ã«æ°æ¥ã»ã©ã®ãã¬ã¼ãã³ã°ãåããããã°ãScalaã使ããããã«ãªãã¨æã£ã¦ã¯ããã¾ãããããããJavaéçºè ã¯ãé¢æ°åããã°ã©ãã³ã°ãä½ããç¥ããªãããããã¾ããããããããããScalaã¯Javaã¨ã¯éãã®ã§ãã
Kotlinã¯å¦ã³ãããè¨èªã§ãããã¾ããããã«å®éã«åããã¦ã¿ããã¨ã¯ããã«ç°¡åã§ãããªã³ã©ã¤ã³ã®å®è¡ç°å¢ããããããã«ã¯ãµã³ãã«ããããããã«Javaã³ã¼ããKotlinã«å¤æãããã¨ãã§ãã¾ãããªã®ã§ãJavaã§ãã£ã¦ãããã¨ãKotlinã§ã©ã®ããã«å®è£ ãããè¯ãããããã«ãããã¾ãã
ãã¼ã«
Scalaã¨Kotlinã¯ã©ã¡ããéçåä»ãã§ãããã³ã³ãã¤ã«æãã§ãã¯ã¨éçåæãã¼ã«ãå©ç¨ã§ãã¾ããçè«ä¸ã¯ãã©ã¡ããåãããã³ã·ã£ã«ãæã£ã¦ããã¨è¨ããã§ããããããããç¾ç¶ãKotlinã®æ¹ããã¼ã«ã¯å å®ãã¦ãã¾ããåã®ç¥ããããã°ã©ãã³ã°ãã¼ã«éçºä¼ç¤¾ã§ããJetBrains社ãKotlinã®éçºå ã§ãããã¨ãèããã¨ãé©ããã¨ã§ã¯ãªãã§ãããããããã¥ã¡ã³ãã«ã¤ãã¦ã¯Scalaãå£ã£ã¦ããã¨ããããã¯ãKotlinã®ããã¥ã¡ã³ããç´ æ´ãããããã¨ãã£ãã¨ããã§ãããã
以åã¯ãScalaã®å ¬å¼ã¦ã§ããµã¤ãã§Eclipseããã¼ã¹ã¨ããIDEãæä¾ãã¦ãã¾ãããä»ã§ã¯ãIntelliJã®Scalaãã©ã°ã¤ã³ã使ããã¨ãå§ãããã¦ãã¾ãã
ä¸æ¹ãKotlinã¯ãã§ã«IntelliJ IDEAã¨Android Studioã«çµã¿è¾¼ã¾ãã¦ãããEclipseã«ããã©ã°ã¤ã³ãç¨æããã¦ãã¾ããããã«ãIntelliJ IDEAã«ã¯åè¿°ã®å¤æãã¼ã«ã¨ãªã³ã©ã¤ã³ãµã³ãã«ããããJavaã®ã¯ã©ã¹ãç°¡åã«å¤æãã¦Kotlinãã¤ã³ã¿ã©ã¯ãã£ãã«å¦ç¿ãããã¨ãã§ãã¾ãããã¡ãããã¹ã¿ã³ãã¢ãã¼ã³ã®ã³ã³ãã¤ã©ã¼ã使ããèªèº«ã®éçºæ¹æ³ã«åããã¦Kotlinã使ããã¨ãã§ãã¾ãã
ã¾ã¨ã
Kotlinã¯éçºå·¥ç¨ã®å ¨ä½ãè¦æ®ãã¦ä½ãããè¨èªãè¨ããã§ããããããæ©è½ãå¿ è¦ãã©ãããã³ã³ãã¤ã©ã¼ã®é度ã«ã©ã®ããã«å½±é¿ãä¸ãããããã¼ã«ãä½ããã¨ãé£ãããªãã ãããããªã©ãåè¨è¨ãã©ã®ããã«éçºã®ã¨ã¯ã¹ããªã¨ã³ã¹ã«ã¤ã³ãã¯ããä¸ããããèãã¦ä½ããã¦ãã¾ãã
Scalaã¯å®ç¨æ§ã第ä¸ã¨ãã¦èãããã¦ããããããã¨ã¯å¥ã®ç®çï¼Javaããå¼·åãªè¨èªãJavaã§ã§ããªããã¨ãã§ããè¨èªãä½ããã¨ï¼ãæã£ã¦ãã¾ããScalaã¯C++ã«ä¼¼ã¦ãã¦ãã¨ã¦ãè±å¯ãªæ©è½ãè©°ãè¾¼ã¾ãã¦ãã¾ãããããããããä¸äººã«ã¨ã£ã¦ä½¿ããããè¨èªãã¨ããã¨ãããã¨ã¯è¨ããªãã®ã§ã¯ãªããã¨çè ã¯æãã¾ãã
Javaãããè¯ãè¨èªãæ±ãã人ãã¡ãããã¦ãJavaã«æºè¶³ããããªãã£ã人ãã¡ãScalaã使ãå§ãã¾ãããåè ã®ã»ã¨ãã©ã¯Scalaã«æºè¶³ãã¦ãã¾ãããå¾è ã¯Scalaã®è¤éãã«å°æããããããã¨ã«ãªãã¾ããããã¯å½ç¶ã®ãã¨ã§ãScalaã¯Javaããã¯å¥ç©ãªã®ã§ãããããã®ãããªäººãã¡ã«ã¯Kotlinãããè¯ããã§ã¤ã¹ã¨ãªãã§ãããã
Â