æ¢åã¯ã©ã¹ãæ¡å¼µ(ããããã«è¦ãã)ã²ã¨ã¤ã®æ¹æ³
Rubyã®ç¹ç°ã¡ã½ãã/ç¹ç°ã¯ã©ã¹ã¿ãããªãã¨ãåºæ¥ãªãã®ããã¨æã£ã¦èª¿ã¹ã¦ããã¨ãããåæ段éã¨ãã¦ãæ¢åã®ã¯ã©ã¹ãæ¡å¼µã§ããããã¨ããç¹ãæã£ã¦ã¿ããã¨ã«ãªãã¾ããã
Scalaã¯ãã¨ãã¨ãRichStringã¨ããã¯ã©ã¹ããããjava.lang.Stringã¯ã©ã¹ã«åå¨ããªã便å©ã¡ã½ãããå®ç¾©ããã¦ãããå¿ è¦ã«å¿ãã¦RichStringã«å¤æããã¦å¼ã³åºããããã«ãªã£ã¦ãã¾ãã
scala> "100" res24: java.lang.String = 100 scala> "100".r res25: scala.util.matching.Regex = 100
java.lang.Stringã«ã¯ r ã¨ããã¡ã½ããã¯å®ç¾©ããã¦ãã¾ãããããã§ã¯æé»ã®ãã¡ã«RichStringã¯ã©ã¹ã¸ã®å¤æãè¡ããã¦ãã¾ããã©ã®ãããªimplicit defãèµ·ããå¾ããããå®è¡æã«ãã¾ãç¥ãæ¹æ³ãããã°ããã®ã§ãããç§ã¯ãããããã¾ãããããããå®é¨ãã¦ã¿ãçµæã
scala> val a = "100".r _ <console>:5: error: _ must follow method; cannot follow scala.util.matching.Regex val a = "100".r _ ^ scala> val a = ("100":RichString).r _ a: () => scala.util.matching.Regex = <function>
ããããæ示çã«è¿½ãããã¦ã¿ãã¨ããã§ãããåè
ãã¨ã©ã¼ã«ãªãã®ãæ²ããã¨ããã§ãã
å¾è
ã§ã¯ã©ã®ã¯ã©ã¹ã«å¤æããããããç¥ã£ã¦ãã¦ããããæ示çã«ç¤ºãã¦ããä¾ã§ããå®éã«ã¯ "r"ã¡ã½ãããæå¾
ãã¦ããã¨ãããã¨ã¯ãä½ãç¹å®ã®class/traitãç¸æã«ãã¦ãããã¨ã¯ééããªãã®ã§ãã»ã©åé¡ã¯ãªãã®ã§ããã
ãã¦ãä¸è¨ã®ä¾ãè¦ã㨠Stringã¯ã©ã¹ã« rã¨ããã¡ã½ããã追å /æ¡å¼µãã¦ããããã«è¦ãã¾ããããã¨åããã¨ãèªåã§ãããããï¼
$ ruby class String def hoge; 100 end end puts "hoge".hoge ^D 100
ã¨ãããããï¼ã¨ããæã«ã©ããããããä»åã®ãã¼ãã§ãã
ããããæ¹æ³ã¯ããããã§ãããRichStringã®ä¾ã«å£ã£ã¦ã¿ã¾ãã
scala> class HogeString(val self:String) { | def hoge = 100 | } defined class HogeString scala> implicit def stringAsHogeString(src:String):HogeString = new HogeString(src); stringAsHogeString: (String)HogeString scala> "fuga" res1: java.lang.String = fuga scala> "fuga".hoge res2: Int = 100
ãã¾ãããã¾ãããï¼ãã¢ã¯ implicit def ã§ãããã®å®£è¨ããããã¹ã³ã¼ãå ã§ã¯å¼æ°åã¨é¢æ°ã®æ»ãå¤ã®åã®éã§æé»ã®ãã¡ã«å¤æããããªããããã¨ããä»æãã®ããã§ããæ£ç´ãããªãã³ã¯ãæ©è½ã§ãããã¨æãã¾ããã
ããã«çªã£è¾¼ãã§ç°¡åã«ããæ¹æ³ãããã¾ã
scala> implicit def stringToHogeable(src:String) = new { def hoge = 100 } stringToHogeable: (String)java.lang.Object{def hoge: Int} scala> "hogehoge".hoge res0: Int = 100
ããã¾ã§æ¥ãã¨ããªãæ¿ããæãããã¾ããã
ä¸è¨ã®ä¾ã§ã¯ implicit defã解éããæç¹ã§ãæ»ãå¤ã¨ãªã£ã¦ããç¡åã®ã¯ã©ã¹ã hogeã¡ã½ããããã£ã¦ãããã¨ã¯ã³ã³ãã¤ã«æç¹ã§åãã£ã¦ããããã§ãããããªãã©ã®implicit def ãå¼ã³åºãã®ãã¯Scalaã«ã¯ãããããã§ããã
ããã§ããããªå®é¨ããã¦ã¿ã¾ãã
scala> implicit def hogeable(src:String) = new { | println("hogeable.new src="+src); | | def hoge = 100 | } hogeable: (String)java.lang.Object{def hoge: Int} scala> implicit def fugable(src:String) = new { | println("fugable.new src="+src); | | def fuga = 200 | } fugable: (String)java.lang.Object{def fuga: Int} scala> "abc".hoge hogeable.new src=abc hogeable.new src=abc res0: Int = 100 scala> "def".fuga fugable.new src=def fugable.new src=def res1: Int = 200 scala> "ghi".naiyo <console>:7: error: value naiyo is not a member of java.lang.String "ghi".naiyo ^
ã¨ããããã§ã
- implicit defå¾ãåå¨ããªãã¡ã½ãããå¼ã³åºããããªã±ã¼ã¹ã§ã¯ãå®ç¾©æ¸ã¿ã®implicit defã¯è©¦ãããªã (ããã¯å½ããåã¨ãããå®è¡å¹çä¸å¥½é½åã§ãã)
- åå¨ããªãã¡ã½ãããå¼ã¶éã«ã¯ãå¿ è¦ãªå¤æã«è©²å½ããimplicit defãã ï¼ã¤ãç¹å®ãã¦å¼ã³åºã
- è¬ãªã®ãããªãä¸è¨ã®ä¾ã§ã¯ new ãï¼åèµ°ã£ã¦ããã®ãã¨ãããã¨ãã¤ã¾ã implicit def ã¯äºåè©ä¾¡ããã¦ããã®ã...?
ã¨ããæãã«ãªãã¾ããã
æå¾ã®ããªãï¼åå¼ã°ãã¦ããã®ããã¯ã¾ã ãããã¾ãã⦠èè ã®ã³ã¡ã³ããããã ããã¨ãããã¼ã§ãï¼
ã¡ãªã¿ã«ç¡åã¯ã©ã¹ã§ãªããã°ä¸åã®ããã§ãã
scala> class Test { | println(this); | | def piyo = 100 | } defined class Test scala> implicit def toTest(a:String) = new Test toTest: (String)Test scala> "hoge".piyo line5$object$$iw$$iw$Test@10c0fa7 res1: Int = 100