ãã®è¨äºãã©ãã ãã®äººã«éè¦ãããã®ã§ãããï¼
æ¦è¦
ã«ãã´ãªã¨ã¯ãæ¢åã®ã¯ã©ã¹ã«å¯¾ãã¦ã¡ã½ããã追å ããæ©è½ãæ¢åã®ã¯ã©ã¹ãåã³ã³ãã¤ã«ããå¿
è¦ããªããã¯ã©ã¹ã®æåãå¤æ´ãããã¨ãã§ããã
ãGroovyã®ã«ãã´ãªã¯ãObjective-Cç±æ¥ã§ããObjective-Cã®ç±æ¥ã¯ç¥ãã¾ããã
ç°¡åãªæ¯è¼
ã¯ã£ãããã£ã¦æ¦è¦ä»¥å¤ã®ç¹ã¯ãå ¨ãéãã¨ãã£ã¦ãããå®è£ ä¸ã®åé¡ãããã¨æãã¾ããObjective-Cå´ããè¦ããGroovyã®ã«ãã´ãªã¯ããæ´ç·´ããã¦è¦ããã©ãããã©ãéå´ããè¦ããæãã¦ä½¿ããªãããããã¾ããã
ããããã®æ¸ãæ¹
// Objective-C @interface NSObject (BetterHash) - (unsigned)hash; @end @implementation NSObject (BetterHash) // åå«ã¯ã©ã¹ã®ãã¡ãç¬èªã®ãªã¼ãã¼ã©ã¤ãã®ãªã-hashã¯å ¨ã¦ãã®å®è£ ã«ç½®ãæãã - (unsigned)hash { return better_hash_function(self); } @end NSObject *obj = [NSObject new]; [obj hash];
// Groovy @Category(Number) class NumberCategory { int hoge() { better_hash_function(this) } } use(NumberCategory) { 100.hoge() }
詳ããæ¯è¼
å®è£ æ¹æ³
Groovyã¯å¥ã®ã¯ã©ã¹ãä½ããã®ã¯ã©ã¹ã®ã¡ã½ããã«ãªãã¸ã§ã¯ãã渡ããã¨ã«ãã£ã¦è§£æ±ºãã¦ããããã§ããããã¯ãã²ã¨ã¤ã®ã¢ããªã§ã¯ããã¯ã©ã¹ã«ããã¦ããã¡ã½ããã¯è¤æ°ã®åããããå¯è½æ§ãããã¾ãã
ãObjective-Cã§ã¯ãã³ã³ãã¤ã«æã«ã¡ã½ããã追å ãä¸æ¸ããã¦ãããããªåãããã¾ããããã¯ãã²ã¨ã¤ã®ã¢ããªã§ã¯ããã¯ã©ã¹ã«ããã¦ã®ããã¡ã½ããã¯ä¸ã¤ã®åãããããã¨ã«ãªãã¾ãã
å±ææ§ã®æç¡
Groovyã«ã¯å±ææ§ãåå¨ãã¾ãããObjective-Cã«ã¯ããã¾ããã
ãGroovyã§ã¯ãuseã«ãã£ã¦ã«ãã´ãªã使ç¨ããç¯å²ãæå®ãããã¨ãã§ãã¾ããããã«ãã£ã¦ä½¿ãããç¯å²ãéå®ãæ··ä¹±ãé¿ããäºãã§ããã§ããããä¸æ¹ã§ãé »ç¹ã«ä½¿ãããå ´åã¯è¨è¿°éãå¢ãããã¨ã«ãªãããã®å ´åã¯å¥ã®ã¡ã¿ããã°ã©ãã³ã°ã§è§£æ±ºãããã¨ã«ãªãã§ãããã
ãObjective-Cã«ã¯ãå±ææ§ã¯ããã¾ãããã³ã³ãã¤ã«æã«ã¢ããªå
¨ä½ã«ã«ãã´ãªãé©ç¨ããã¾ãã使ããã¨æãã°ãimportããªãã¦ã使ãã¦ãã¾ãã®ã§ãã«ãã´ãªã«é¦´æã¿ã®ãªã人ã«ã¨ã£ã¦ã¯ãé常ã«ãããã«ããããããã¾ããã大æµã®å ´åã¯è¨è¿°éãæ¸ãã®ã§ãObjective-Cã®äººã¯å¥½ãã§ä½¿ã£ã¦ããããã§ããå±ææ§ã®ããã¡ã¿ããã°ã©ãã³ã°ã®æ¹æ³ã¯ç§ã®ç¥ãéãã§ã¯åå¨ãã¾ããã
è¤æ°ã®ã«ãã´ãª
è¤æ°ã®ã«ãã´ãªã§åãã¡ã½ãããå®è£
ããå ´åãã©ã®ãããªæåã«ãªããï¼
ãGroovyã§ã¯ãè¤æ°ã®ã«ãã´ãªãåãã¡ã½ãããå®è£
ãã¦ããã¨ãã¦ããuseã«ãã£ã¦ã«ãã´ãªã使ãåãã¤ãã¨ãã§ãã¾ããããã«ãè¤æ°ã®ã«ãã´ãªãåæã«ä½¿ã£ãå ´åã¯æå¾ã«useããããã®ãåªå
ããã¾ãã
ãObjective-Cã§ã¯ãè¤æ°ã®ã«ãã´ãªãåãã¡ã½ãããå®è£
ãã¦ããå ´åãã©ããä¸ã¤ãå®è£
ãã使ãåãããã¨ãã§ãã¾ãããã©ããå®è£
ããããã¯ãªã³ã¯ã®é çªã«ä¾åããã¨æããã¾ããå®è³ªã©ã®ã«ãã´ãªã使ãããããããã¾ãããè¤æ°ã«ãã´ãªã使ç¨ããå ´åã¯æ³¨æãå¿
è¦ã§ãã
ã¾ã¨ã
Groovyã®ã«ãã´ãªã¯ç´ æµã ãã«ãã´ãªã¨åä¹ã£ã¦ãã®ã¯ããªã«ãéãæ°ããã¾ã