implicit conversionãå®ç¾©ããéã«æä½éãã§ãã¯ãã¹ããã¨
軽ãã¤ã©ãâã£ã¨ããã®ã§æ¸ãã
Scalaã«ã¯implicit conversionã£ã¦ã®ããã£ã¦ã¾ãç¥ããªããªãã°ã°ããã§ãimplicit conversionãå®ç¾©ããéã«ãæä½éãã§ãã¯ãã¹ããã£ãã²ã¨ã¤ã®ãã¨ãæ¸ãã
ãscala.Predefã§implicit conversionãå®ç¾©ããã¦ããåã«å¯¾ãã¦æ°ããimplicit conversionãå®ç¾©ããéã¯ãPredefã§å¤æãããåã¨ãæ°ããå®ç¾©ããå¤æå ã®åã¨ã§ã¡ã½ããåãéè¤ããªãããã§ãã¯ããï¼ï¼ã
å ·ä½ä¾ã§è©±ããscala.Predefã«ã¯StringããStringOpsã¸ã®implicit conversion(ããé·ãã¦ã¦ã¼ã§ã®ã§ä»¥ä¸ICãª)ãå®ç¾©ããã¦ãããStringOpsã£ã¦ã®ã¯æååæä½ã«é¢ãããããã便å©ã¡ã½ãããè©°ã¾ã£ã¦ãtraitã ã
implicit def augmentString (x: String): StringOps
ã§ã StringOpsã«ã¯linesã£ã¦ã¡ã½ããããããStringããè¡ã®Iteratorãä½ã£ã¦ãããé常ã«ãã使ãã¢ã¬ãªã
scala> val str = Seq("aaa","bbb","ccc").mkString("\n") str: String = aaa bbb ccc scala> val strops = Predef.augmentString(str) strops: scala.collection.immutable.StringOps = aaa bbb ccc scala> strops.lines lines linesIterator linesWithSeparators scala> strops.lines res7: Iterator[String] = non-empty iterator scala> str.lines res8: Iterator[String] = non-empty iterator
augmentStringã§StringOpsã¸ã®ICãããããããããªãstr.linesã¨ãèªãã§ã大ä¸å¤«ã«ãªã£ã¦ããããã¾ã§ã¯ã¾ãããããã
ã§ã¯ãããã§ããããã«ä¾¿å©ããã¦æ±æ¼ãããã¨è©å¤ã®scala.sys.processããã±ã¼ã¸ãimportãã¦ã¿ãã
Scala 2.9.0のscala.sys.processパッケージが便利過ぎる件について - Scalaとか構文解析についてあれこれ書く日記
scala> import scala.sys.process._ import scala.sys.process._ scala> str.lines <console>:12: error: type mismatch; found : str.type (with underlying type String) required: ?{val lines: ?} Note that implicit conversions are not applicable because they are ambiguous: both method augmentString in object Predef of type (x: String)scala.collection.immutable.StringOps and method stringToProcess in trait ProcessImplicits of type (command: String)scala.sys.process.ProcessBuilder are possible conversion functions from str.type to ?{val lines: ?} str.lines ^
ãã£?ãã£ãã¾ã§ã¡ããã¨ä½¿ãã¦ããStringOps#linesã使ããªãã?ãªããªãã ã?
ããã¯ãã¨ã©ã¼ã¡ãã»ã¼ã¸ã«ãããããã«ãscala.sys.process.ProcessImplicitsã«ãã£ã¦Stringãã ProcessBuilderã¸ã®ICãå®ç¾©ãããããã ã
implicit def stringToProcess (command: String): ProcessBuilder
scala.sys.process.ProcessBuilderã«ã¯ãStringOpsã¨åãlinesã£ã¦ååã®ã¡ã½ãããå®ç¾©ããã¦ãããã ãããåã«Stringã«å¯¾ãã¦linesãå¼ã³åºããå ´åã¯ãã³ã³ãã¤ã©ãaugmentStringãstringToProcessãã©ã¡ãã®ICãé©ç¨ãã¹ããå¤æã§ããªãã®ã§ã³ã³ãã¤ã«ã¨ã©ã¼ã«ãªã£ã¦ãã£ã¦è¨³ã ããã¡ãã¯ï¼ï¼æ°ãï¼ï¼
ãããåé¿ããããã«ã¯ã以ä¸ã®ããã«åã¢ããã¼ã·ã§ã³ãä»ãã¦ã¡ã½ããå¼ã³åºããã¦ããã°åé¿ã§ãããããæ¬æ¥ã¯ã©ã¤ãã©ãªä½æè ã注æããã°ããã ãã®è©±ã ã
scala> (str:StringOps).lines res14: Iterator[String] = non-empty iterator
ãã®ãããªICã§ã®ã¡ã½ããåéè¤ã«ããå½±é¿ã¯ãæãããããã¨ã«Scalaã®SDKå ã§ãçµæ§ãããå ãã¦ãspecs2ãªã©ã®èåãªã©ã¤ãã©ãªã§ããããã¡åã£ã¦ãã¾ããã¯ã½ãã
ä»ã®ã¨ããããã®ãããªéè¤ãã¦ã¼ã¶ã¼å´ã®ã³ã¼ãã§è§£æ±ºãããã«ã¯åã¢ããã¼ã·ã§ã³ãããªããå°æ¥ã®Scalaã§ãICã«é¢ããããå°ãç´°ãããªå¶å¾¡(åªå 度ä»ãããã¹ã³ã¼ãå ã®ICã®ç¡å¹å)ãªã©ãã§ããããã«ãªãã¨ããããã¡ããã³ï¼ï¼
ããã´ã¼ã£ã¯ãã£ããã£ãã
ãï¼ï¼ãããâ¦ã»ã«ãã£ãã
ãã
ï¼ã
â¦ãã
ãã¼ã¼ï¼ï¼ï¼ãã£ããï¼ï¼ï¼ãããï¼ããã¼ã£ãã
ããã¯ã«ãã£ã±ããã¼ã¼ï¼ï¼ã£ãã
ãï¼ï¼ï¼ï¼ï¼ï¼ã¬ãï¼ï¼ãã±!ã¢ãã¢ãã¢ãã¢ãï¼ï¼ãã¬ããµã«ããï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ãããããï¼ãã
ããµããã£ãã
ããã£ã¯ã¼ã¼ã£â¦ããï¼ï¼ããµããããã¼ã£ã»ãããããããã²ããã¼ã£ãã£ï¼ãããâ¦ãµã¼ã¼ï¼ã£ãããã£ãã
ããã©ãã·?ããã±ã¼ï¼ã¢ãã¢ãï¼ããºãºãºï¼ã
â¦ãâ¦ãããã¯ã«ãã¼ã£ï¼ãããã£ï¼ã¬ãï¼ã¯ã«ã
ããããã©ããã¼ï¼ã¸ãºã£ãããã£ã»ã«ã
ããµãããã½ã£ããã¼ã£ã¼ï¼ãããã¬ãããã¯ã«ãã±ã¼ï¼ãããï¼ï¼ï¼ï¼ã¬ãã±â¦ããã´ã¼!ããã£ï¼ãã
ï¼ï¼ï¼ï¼ï¼ãã±ï¼ãã¶ããã¯ã«ã
ããâ¦ãï¼ï¼ï¼ãµãµã¬ãã¼ï¼ã¬ããã¼ï¼ããï¼ãã£ããããããã¼ã£ã¼ã¼ããã
ããµã¬ãµ!ãããï¼ï¼ï¼ï¼ï¼ãï¼ï¼ï¼ï¼ï¼ï¼ã«ãã¼ã£ã»ã«ã
ããããµãµã¬ãµãããã²ã?ããã½ã£ãï¼ãã
ã
ï¼ï¼ï¼ï¼ï¼ã£ããã¼ã¼ã¼ãã¸ãºãºãºã£ï¼ï¼ï¼ï¼ããã£ã±ããã¼?ãµã
â¦ããã©ãã·ããã£ã»ã«ãã¼ï¼ãµãã¼ã¼ã¼ã£ããï¼ï¼ï¼ï¼ã¬ãã½ã£ã±ã¼ï¼ã£ãããµãã¼ï¼ãï¼ï¼ã¸ãºã£ãï¼ã¯ã¼ï¼ï¼ãããâ¦ãã
ï¼ããã·ï¼ããã¼ã¼ã¼ï¼ã¸ã·ãã¢ãï¼ï¼ï¼ã¯ãã¼ï¼ï¼ï¼ãããâ¦ãâ¦ããã¸ã·ããããã£ã»ãããããã£ã»ã«ãã±ã¼ï¼ï¼ãã¬ããï¼ã£ã»ãããºã£ã¼ï¼ã¢ãã¢ãã¢ãï¼ï¼ï¼ãããããã¼ï¼ï¼ï¼ã¯ã«ãã±ããï¼ã«ãã£ã±ããã±ãããã£ã±!ããã¼!ããã£â¦ããã´ã¼ã£ãã£ã»ã«ã
ãããã¼ããµã«ãã¼ã¼ã¼ã¼ï¼ã¸ã¬ãï¼ãããã£ãããµãã¼ã£ã»ãããº!ãï¼ã£ã±ã¼ã£ï¼ããããããããï¼ãã¶ãã¼ã¼ãããâ¦ã£ãã
ããµãããã£ããã¼ã£ã»ãããã½ã£ã»ãããï¼ã¬ããµï¼ã²ããããããã£ï¼ï¼ï¼ãï¼ï¼ã£ã»ã«ãã¼ï¼ï¼ï¼ã²ãã£ã»ã«ã
ãã¼ã¼ã£ã¯ã«ã
ãã£ã±ããã£â¦ããã£â¦ããã±ãã£â¦ããããããããã¼ï¼ã¬ãµãµãµã
ã
ããµã
ï¼ï¼ï¼ï¼ã
ãï¼ã£ã¯ã«ãã¼ï¼ãï¼ï¼ãã£!ã¯ãããã»ããããã£ï¼ã»ã«ã
ãã¯ãâ¦ããã©ãã·ããããã
ããã©ããããã£ããã´ã¼ã¼ï¼ã²ããã±ãã¼ã¼ã¼ã¼ï¼ãµã¼ã£ãâ¦â¦ããï¼ã¬ãããããï¼ã¬ãããã£ã¯ã¼ãã¸ãº?ãµããâ¦ãããµã¬ãµã¬ãµ!ãããã¼ï¼ã²ãããâ¦ãã
ããããããã¯ã«ã
ããã
ãã¼ï¼ã¯ã«ã
ãï¼ã¢ãï¼ï¼ãããããããã²ãã´ã¼ã£ãã£ããã£â¦ãããï¼ï¼ã²ãã±ãâ¦ãâ¦â¦ãã²ãããã±ï¼ã»ãããºããã¶ããï¼ï¼ãã
ï¼ããµã¬ãµ!ã£ãâ¦ããµã
â¦â¦ããã
ãã£ã»ããããã£ããâ¦ããã£ããµã¬ãµã¼ãããµã¼ã£ããµã¼ï¼ãã£ãï¼ãããï¼â¦ããâ¦ãâ¦ãã¬ãã±ããã¼ï¼ï¼ï¼ï¼ã¢ãã¢ãã¢ãï¼ããâ¦ããã¯ã¼ã£ã±?ãã
ã
â¦ãããã©ãããâ¦ãããºã£ããµã«ã
ããã¼ï¼ï¼ã¯ã¼ã¼ãã¸ãºã£ï¼ããã¸ã¬ãã£ã¯ã¼ãããããã«ãã±ã¼ã¼ã£ãã£ï¼ã»ã«ãã±ãã¼ã¼ã¼?ããã·ãã£ãã£ã¼ãâ¦ãã¯ãï¼ã£ããµã¼ï¼ããµã¬ã?ããµã¼ã¼ã£ãã²ãããããã!ãã£ããã©ãã·â¦ããã¼!ãããããã¼ã¼ã¼ï¼ï¼ã¬ããã£ãã
ãï¼ï¼ããã£ããï¼ã²ãã´ã¼ï¼ã¸ã¬ããµã¬ããâ¦ã¸ã·ãããããã¯ã¼ã£ï¼ã¬ãããã£ã¼ï¼ï¼ï¼ï¼ã
ï¼ã¬ãã£ãï¼ï¼ï¼ãããã£ãããï¼ãã¼ã£ãâ¦ããã©ãããã¼ã¼ã¼ï¼ãµãã£ã¼!ã£ï¼ï¼ã«ãã¼ã¼ã£ã¼ã¼ã£ãããâ¦ãï¼ãï¼ãã£â¦ãããããã
â¦ããããã£ããã£ã»ããããã¯ã«ãã¼ã£ãã
ãï¼ã«ãããµãããããã¯ãâ¦ããããã¼ã¼ãâ¦ãã
ããµã«ãã´ã¼ã£â¦ãããããã¯ã¼ã£ã¼ã£ã»ã«ã
ããµããµãï¼ã»ã«ãã±ã¼ï¼ï¼ï¼ãã¬ãã¼ã£ãâ¦ãâ¦ããã·ãï¼ããã
ï¼ãï¼ï¼ï¼ï¼ï¼ãããã½ã£ï¼ã¸ã¬ãï¼ï¼ï¼ï¼ï¼ã¢ãã¢ãã¢ãï¼ï¼ãã
ãã¯ã«ãã¼ã¼ã£â¦ããã£ï¼ã«ãã¼ããããããããããã£â¦ããï¼ãã!ã
ãããã¼ãã£ã¼ã¼ï¼ï¼ã¢ãã¢ãï¼ï¼ãï¼ï¼ãã±ããã¼ã¼ï¼ããããã¼ã¼ã¼ã¼ã¼ï¼ï¼ã»ããããããâ¦ããã¼ã£ãã
ï¼ã¸ã¬ããããâ¦â¦ããï¼ããµã¼ã£ã¯ããã²ãã´ã¼ã¼ï¼ï¼ã¸ã·ãã¼â¦ã¬ãã·ãããºãºãºï¼ãã¼ï¼ï¼ãã£!scala> println(g) ã¬ãã£ï¼ãã¼ï¼ï¼ï¼ã¢ãã¢ãï¼ï¼ããã½ã£ããµãµãããããã¢ãã¢ãï¼ï¼ãã±ã¼ã¼ã£ï¼ï¼ãã¯ã«ãã´ã¼ãããµ?ã«ãã´ã¼ã¼ã¼ï¼ãããµ?ããµããã£ã»ããããºãºâ¦ãã¼ï¼ã¢ãï¼ãï¼ã£ã¼â¦ã£ãã¼ï¼ã²ãã¼ï¼ããããâ¦ããã¯ã«ããã²ããã¼ã¼ã¼ã¼ã¼ï¼ãããã£ããã¼ãã¬ãããâ¦ãããã£ãããã¼ã£ãããã±ãã¼ã£ããããã©ãã·ããã£ï¼ï¼ï¼ã»ã«ã
ããã¯ã«ãã¼ã£ããã£ãã
ãã¯ã¼ã¼ã¼!ã
â¦ãã
ãï¼ãããã¼ï¼ï¼ã
â¦ãã¯ãã¼ï¼ï¼ï¼ï¼ã¬ãã·ããã£ãã
â¦ããã¼ãã»ã«ãã¼ï¼ã
ï¼ããã¯ã«ãã±ããã£ã¼?ã¸ãºãºãºã£ã±ããã¼ã£ãã²ãããããã¼ï¼ã¢ãã¢ãã¢ãï¼ï¼ã¬ãããâ¦â¦â¦ããã¯ãããâ¦ã¯ããâ¦ãµãµããã¶ãããã£ï¼ããã©ãã½ã£ãã¢ãï¼ããã«ãã±ãããï¼ï¼ï¼ï¼ãã£â¦ããï¼ã¬ãããâ¦ããããã¯ã«ãããµãµãµãµã«ã
ãã£ã»ã«ã
ããµãµã¼ï¼ï¼ãã?ã£ããããµ!ãã
ãï¼ã¢ãï¼ãããã¼ã¼â¦ããã½ã£â¦ããï¼ï¼ï¼ãã£ã±ãããµãã¼ï¼ã¯ã«ã
ãã£ãã£â¦ããã¯ã¼ã¼ãããã£ããµã?ã£ã¯ãï¼ããããããããµããã
ãã£ãããã£ã±ããã±ã¼ã£ï¼ããããâ¦ã¸ã·ï¼ã¯ãã£ã¯ã¼ã¼ï¼ããºï¼ããã¼ãããï¼ã«ããã
â¦ããããã?ãã
ãããï¼ï¼ããµãããï¼ï¼ï¼ï¼ï¼ãã
ã
ï¼ã¯ã¼ã£â¦â¦ããï¼ãããããã²ããã±ãã¼ã¼?ãã
ããã£ï¼ï¼ï¼ããã£ã»ããï¼ããããã²ããããã£ãï¼ããã½ã£ã¼!ãã¶ããâ¦ãããããã
ãããã
ãããµã
â¦ããããã¯ãâ¦ããºã£ï¼ããµã¬ãããã£ããã´ã¼ï¼ã»ã«ãã±ãããã£ãï¼ï¼ï¼ï¼ã²ãããã±ããããããã¯ãã¼ã¼ã¼ï¼ã¸ãº!ããããâ¦ãã£!ã¬ããã£ã¼ï¼ã
ããµããã¯ããã¯ã«ã
ãï¼ã¬ããããã±ã¼!ããã£ãããµãï¼ãã²ãã¼ã¼ã£ï¼ã¯ãããã£ã¼ã¼ã£ããã»ãããã½ã£ã¼ï¼ã
ãã
ãã
ï¼ï¼ããã£â¦ãï¼ã£ã¼ã¼ï¼ããããããããã»ã«ãã±ã¼ã¼?ãµã«ã
ãã¼ãã£ã±ï¼ã¯ã¼ï¼ã»ã«ããã
â¦â¦ããããã£â¦ãï¼ï¼ã²ããã´ã¼!ã
â¦ã!ãã©ãã·ãããã²ããã¼ã¼ã£ãâ¦ããã©ããããããï¼ã£ãã²ãã¼ï¼ï¼ï¼ï¼ããããã©ãã·ããããããã¼ï¼ï¼ï¼ï¼ããºãºãºâ¦ãã¶ããï¼ã«ãã±ããããããâ¦ãã£ã¼ï¼ãã
ãï¼ï¼ãã
ã
ã
â¦ã¬ããã¼ããã
ããã
ãã£â¦â¦ãã£ãï¼ãããâ¦ããï¼ãã
ã
ã
â¦â¦ãã
ããããã!ã!ããããã²ããã£ï¼ããã£ãããµâ¦ã?ã¬ãµããâ¦ããã©ãã·ãã¯ãã£ãããµ?â¦â¦ãããï¼ãã¬ããã£ããµã¬ãããããâ¦ãã£ããâ¦ããããã¯ã¼ï¼ããããã»ãããºãºãºã£ãããããããï¼ãããããã£ã»ããï¼ï¼ããº?ã¸ãºããã!ããï¼ï¼ï¼ããºã£â¦ããµã¬ãã£ãã²ããã±ã¼ã£ããã£ãâ¦ãã£ãï¼ï¼ãã¬ãã¼ãããºâ¦ã?ãããããã£ã¼ï¼ï¼ï¼ããã£ã¼ã£ã»ã«ããããµã«ãã¼ã£ãâ¦â¦ããããããã£ã±ããã£ããï¼ã«ã
ãã¼ã£ãâ¦ããµã¬ããã£ãããããâ¦ã¢ãï¼ï¼ãã±ã¼ï¼ãµã¬ãããã©ãã½ã£â¦ããããã¯ããããã¯ã¼ã¼ã¼ï¼ã
â¦ããã©ãã·?ãã¬ãããâ¦ãã
ãï¼ã«ãã±ããã´ã¼ã£â¦ã¯ãã¼ã£ï¼ã¢ãï¼ã«ã
ããã
ãã£ã±!ãããµãµãã¬ãï¼ãããâ¦â¦ãã
ããµã
ã
â¦ãâ¦ããï¼ï¼ããºãºãºã£â¦ãã£ï¼ãï¼ããã¼ã¼ï¼ã¢ãã¢ãï¼ï¼ï¼â¦ãããããããã£ããï¼ï¼ï¼ãããµã«ãã¼ã¼ããâ¦ãããµã¬ããâ¦ãããããããï¼ï¼ï¼ããã¯ã¼ã¼ã¼ããã
â¦ãããã²ãã£â¦ãã¯ã«ãã¼ã¼ã£ããã¼ããããï¼ã£ï¼ã¬ãããã
â¦ãï¼ããã±!ãã
â¦ã«ã
ãããã£ãï¼ï¼ï¼ï¼ï¼ï¼ãµãµãï¼ãã¯ãã¼ï¼ï¼ã¯ã«ãã´ã¼ï¼ï¼ï¼ã¯ã¼ï¼ï¼ã²ãããã£ãã¢ãï¼ï¼ï¼ï¼ï¼ãã
â¦ãã
ãããã©ãã·ï¼ãã£ããµï¼ãã
â¦ãã£â¦ããã£ãããï¼ãããããã¼ã¼ã¼ã¼ãããï¼ã¸ã¬ãããã¶ãã¼ã£â¦ã²ãã£ã»ããï¼ããããã¼!ãã£ã±ããã±ããâ¦ãï¼â¦ãã
ã
ã
â¦ããâ¦ããâ¦ã¯ã«ãã±ï¼ãã
ï¼â¦ãããã!â¦ããã
ãã£ãã
ã
ããã£ã¼ãããï¼ããã¼ã£ã¼ï¼ï¼