ã¤ãã«Kotlinãã¤ã«ã¹ãã¼ã³12ããªãªã¼ã¹ããã¾ããï¼ å ¬å¼ããã°ã¯ãã¡ãã è¨èªä»æ§ã®å¤æ´ã«ã¤ãã¦ã¾ã¨ãã¾ãã ä»åã¯ããã¨å¤ãã§ãã
@ngsw_taro å¤æ´å¤ãã¦ä¿®æ£ã大å¤ãã¨æããã¾ãããAnalyze -> Code cleanupã§ããã¸ã§ã¯ãå
ã®Kotlinã½ã¼ã¹ãã¡ã¤ã«ãã¹ã¦ã«å¯¾ãã¦deprecatedãªã³ã¼ããæ°ããæ§æã«å¤æãã¦ããã¾ãã
— ããã (@ngsw_taro) May 30, 2015
ã¢ããã¼ã·ã§ã³
ã¢ããã¼ã·ã§ã³ã®è¨æ³ãJavaã£ã½ããªãã¾ããã
ã¤ã¾ãM12ããåã§ã¯[Foo(args)]
ã¨æ¸ãã¦ãããã®ããM12以éã§ã¯@Foo(args)
ã¨æ¸ããã¨ã«ãªãã¾ãã
ä»ã¾ã§éã@
ãä»ããã«æ¸ããã¨ãããã¾ãã
ã©ãã«
ãã®ã¢ããã¼ã·ã§ã³ã®å¤æ´ã«ä¼´ããã©ãã«ã®æ¸ãæ¹ãå¤ããã¾ãã
ä»ã¾ã§ã¯@name
ã ã£ããã®ãname@
ã¨@
ãæå¾ã«æã£ã¦ããå¿
è¦ãããã¾ãã
loop@ for (i in 1..100) {
for (j in 1..100) {
if (...)
break@loop
}
}
ã¢ããã¼ã·ã§ã³ã§KClassã使ããããã«ãªã£ã
ã¢ããã¼ã·ã§ã³ã®å¼æ°ã¨ãã¦åãåãã¯ã©ã¹åç
§ããM12ããåã ã¨java.lang.Class
éå®ã§ããã
ãã®ããå¼æ°ã渡ãå´ã®ã³ã¼ãã¯javaClass<MyClass>()
ã®ãããªã¡ãã£ã¨ã¤ã±ã¦ãªãè¨è¿°ã«ãªã£ã¦ãã¾ããã
M12ããã¯java.lang.Class
ã®ä»£ããã«kotlin.refrect.KClass
ã使ããããã«ãªãã¾ãã
ãã©ã¤ããªã³ã³ã¹ãã©ã¯ã¿
ä»ã¾ã§ãã©ã¤ããªã³ã³ã¹ãã©ã¯ã¿ã¯class User(val id: Long)
ã®ããã«è¨è¿°ãã¦ãã¾ããã
M12ã§ããã®è¨æ³ã¯æå¹ã§ãããfull formã§è¨è¿°ããã¨class User constructor(val id: Long)
ã«ãªãã¾ãã
ãã®full formã¯ããã©ã¤ããªã³ã³ã¹ãã©ã¯ã¿ã«å¯¾ãã¦ã¢ããã¼ã·ã§ã³ã修飾åãä»ããã¨ãã«ä½¿ç¨ãããã¨ã«ãªãã¾ãã
ã¤ã³ã¿ãã§ã¼ã¹
trait
ãã¼ã¯ã¼ãã¯éæ¨å¥¨ã¨ãªããinterface
ãã¼ã¯ã¼ãã«åã£ã¦ä»£ãããã¾ãã
åæå
Javaã®enumã¨é常ã«è¿ããªãã¾ããã ã¤ã¾ããã¨ã³ããªéã¯ã«ã³ãã§åºåããã¡ã³ããæãããå ´åã«ã¯æå¾ã®ã¨ã³ããªã«ã»ãã³ãã³ãç½®ããã¨ã³ããªã®ç´å¾ã«ã³ã³ã¹ãã©ã¯ã¿ã¸ã®ãã©ã¡ã¼ã¿ãªã¹ããè¨è¿°ã§ããããã«ãªãã¾ããã詳細ã¯ãã¡ãã
é¢æ°å
é常ã®é¢æ°åãè¦æ±ãããå ´é¢ã§ãæ¡å¼µé¢æ°ã渡ããããã«ãªã£ããããã§ããã¡ã½ããåç
§ãJavaã®ããã¨åããããªæãã«ãªã£ãã¨ãããã¨ã§ããå
·ä½çã«ã¯(String) -> Int
ãè¦æ±ãããç®æã§String::length
ã®ãããªString.() -> Int
åã®é¢æ°åç
§ã渡ãã¾ãã
// M12ããåã¯ããæ¸ãã¦ã strs.map { it.length() } // M12ããã¯ããæ¸ãã (M12ããåã¯åã¨ã©ã¼ã«ãªã£ã¦ã) strs.map(String::length)
KotlinでJavaみたいなメソッド参照をする - 算譜王におれはなる!!!!ã§é¢ç½ã(ãã©è¤éãª)ã¢ããã¼ããèãã¦ããã®ã§ãã®æ¹åã¯å¬ããã§ãï¼
ã¹ãã¼ããã£ã¹ã
ã¹ãã¼ããã£ã¹ã(nullãã§ãã¯å«ã)ãããè³¢ããªãã¾ããã
var
ãªå¤æ°ã«å¯¾ãã¦ãã³ã³ãã¤ã©ã追ããç¯å²ã§ãã¹ãã¼ããã£ã¹ããæå¹ã«ãªãã¾ãã
ååä»ããªã¿ã¼ã³
é«éé¢æ°ã®å¼æ°ã¨ãã¦ç´æ¥è¨è¿°ããé¢æ°å¼å
ã§ã®ååä»ããªã¿ã¼ã³ããµãã¼ããããããã§ã(M11ã§ãããã¦ããããªï¼)ã
é¢æ°å¼ã¯fun(arg)=...
ã®ããã«æ¸ãé¢æ°ãªãã©ã«ã¿ãããªãã¤ã®ãã¨ã§ãã
åé¤ãããæ©è½ã¨éæ¨å¥¨ã«ãªã£ãæ©è½
åé¤
class object
ã代ããã«companion object
ã使ãã¾ãããã- å¿åã¤ãã·ã£ã©ã¤ã¶ã
init
ãä»ãããããã¯ã使ãã¾ãããã
éæ¨å¥¨
when
ã§ã®break
ã¨continue
- ã¤ã³ã¿ãã§ã¼ã¹ã®ã¯ã©ã¹ç¶æ¿
- ã¹ã¼ãã¿ã¤ãã®å ±å¤ç¹æ®å
- éçåã®è¡¨æ