scala操作符优先级

来源:互联网 发布:淘宝怎么修改宝贝图片 编辑:程序博客网 时间:2024/05/21 21:46

关于scala操作符的优先级等问题可以详见这里,本文直接去扒了scala反射中的Toolbox里面负责编译的源代码,找到了其中和操作符相关的部分,记录如下:

对于操作符优先级,有第一个字符决定,数越大优先级越高

 def precedence(operator: Name): Int =   if (operator eq nme.ERROR) -1   else {     val firstCh = operator.startChar     if (isScalaLetter(firstCh)) 1     else if (nme.isOpAssignmentName(operator)) 0     else firstCh match {       case '|'             => 2       case '^'             => 3       case '&'             => 4       case '=' | '!'       => 5       case '<' | '>'       => 6       case ':'             => 7       case '+' | '-'       => 8       case '*' | '/' | '%' => 9       case _               => 10     }   }

对于中缀操作符的结合性,由最后一个字符决定,:则右结合,否则左结合

def isLeftAssoc(operator: Name) = operator.nonEmpty && (operator.endChar != ':')
0 0
原创粉丝点击