折腾:
【未解决】优化游戏规则生成脚本ruleClean的scala代码和逻辑
期间,已有scala代码:
def initMatch(rulePath: String, urlMatch: URLMatcher): Unit ={ val file = Source.fromFile(rulePath) for(l <- file.getLines()){ val row = l.split("\\|",2) 。。。
其中:

rulePath是string
但是:getLines没有任何输出,直接输出错误
ERROR url :

之前有搜过:
scala source fromfile
其中:fromFile 有多种参数类型
都是返回:BufferedSource
scala file getLines
鼠标+control,可以看到定义:

BufferedSource scala.io
override def getLines() Iterator[String]
然后对于 短横线+左箭头的: <-
之前没见过,感觉是 iterator操作?
去找找
scala file getLines ERROR url
scala getLines() ERROR url
import scala.io.Source val filename = "fileopen.scala" for (line <- Source.fromFile(filename).getLines) { println(line) }
难道是:getLines 不带括号?
去试试
// for(l <- file.getLines()){ for(l <- file.getLines ){
结果:
问题依旧。
scala getLines() ERROR url
scala getLines()
好像是 IDE调试debug时(invoke variable toString)导致代码问题?
那不加断点,直接运行试试
def initMatch(rulePath: String, urlMatch: URLMatcher): Unit ={ val file = Source.fromFile(rulePath) for(l <- file.getLines()){ // for(l <- file.getLines ){

然后:

真的可以了。
顺带再去看看:
defgetLines(): collection.Iterator[String] Returns an iterator who returns lines (NOT including newline character(s)). It will treat any of \r\n, \r, or \n as a line separator (longest match) - if you need more refined behavior you can subclass Source#LineIterator directly. Definition Classes BufferedSource → Source

【总结】
此处,对于scala代码:
def initMatch(rulePath: String, urlMatch: URLMatcher): Unit ={ val file = Source.fromFile(rulePath) // for(l <- file.getLines ){ for(l <- file.getLines()){ val row = l.split("\\|",2)
如果给:
for(l <- file.getLines()){
加了断点,则:
导致代码运行出问题,输出:
ERROR url :
原因:IDE(IntelliJ IDEA)调试时,会尝试查看变量信息,会导致触发变量toString的操作,导致变量此处失效。导致后续代码运行报错。
解决办法:不要给这行加断点
感慨:感觉不影响这么弱智和垃圾。
如果真是,那真无语了。
感觉应该有更好办法。
暂时懒得深究了。
【后记】
看到后续代码:
def initMatch(rulePath: String, urlMatch: URLMatcher): Unit ={ val file = Source.fromFile(rulePath) // for(l <- file.getLines ){ for(l <- file.getLines()){ val row = l.split("\\|",2) if(row.size < 2){ println(s"ERROR url : ${l}") }else{ urlMatch.addPattern(row(0), row(1)) } } }
才明白:
原来此处报错:
ERROR url :
是自己此处代码的特定信息
-》不是通用的逻辑。那怪搜不到呢。。。