ScalaとJavaでシフト演算子の優先度が違うのは、決して嫌がらせではなくて「Scalaの優先度は、メソッド名の先頭の1文字で決まる」ルールから来ているようです。
素人目線だと2文字見れば良かったんじゃ…?と思いますが、そうすると何か大変なことがあったのでしょう。たぶん。
それはさておき、Scalaで中間記法を使ったときの優先度は、低い順から、
(all letters) 注: アルファベットとか
|
^
&
< >
= !
:
+ -
* / %
(all other special characters) 注: ~ とか ? とか
注: Scala言語仕様Version 2.8の6.12.3 Infix Operationsより
となっています。シフト(<<)やビット演算(& |)は、比較演算(== !=)よりも「優先度が低い」はずです。
scala> 1 & 2 == 5 <console>:8: error: overloaded method value & with alternatives: (x: Long)Long <and> (x: Int)Int <and> (x: Char)Int <and> (x: Short)Int <and> (x: Byte)Int cannot be applied to (Boolean) 1 & 2 == 5 ^ (& が優先なら) scala> (1 & 2) == 5 res40: Boolean = false (== が優先なら、こっちが期待値) scala> 1 & (2 == 5) <console>:8: error: overloaded method value & with alternatives: (x: Long)Long <and> (x: Int)Int <and> (x: Char)Int <and> (x: Short)Int <and> (x: Byte)Int cannot be applied to (Boolean) 1 & (2 == 5) ^
ビット演算の方が優先度が低いので、上記の例のように「1 & (2 == false)」と解釈されて、型エラーになります。
scala> 1 < 2 == false res41: Boolean = false (< が優先なら) scala> (1 < 2) == false res42: Boolean = false (== が優先なら、こっちが期待値) scala> 1 < (2 == false) <console>:8: error: overloaded method value < with alternatives: (x: Double)Boolean <and> (x: Float)Boolean <and> (x: Long)Boolean <and> (x: Int)Boolean <and> (x: Char)Boolean <and> (x: Short)Boolean <and> (x: Byte)Boolean cannot be applied to (Boolean) 1 < (2 == false) ^
比較演算の方が優先度が低いので、「1 < (2 == false)」と解釈され、型エラーになるかと思ったら、なりません。意味がわかりません。
< | 2014 | > | ||||
<< | < | 01 | > | >> | ||
日 | 月 | 火 | 水 | 木 | 金 | 土 |
- | - | - | 1 | 2 | 3 | 4 |
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 | - |
合計:
本日:
管理者: Katsuhiro Suzuki(katsuhiro( a t )katsuster.net)
This is Simple Diary 1.0
Copyright(C) Katsuhiro Suzuki 2006-2023.
Powered by PHP 8.2.15.
using GD bundled (2.1.0 compatible)(png support.)