コグノスケ


link 未来から過去へ表示  link 過去から未来へ表示(*)

link もっと前
2014年1月26日 >>> 2014年1月26日
link もっと後

2014年1月26日

Scalaの中間記法の優先度

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/26 03:48)

コメント一覧

  • コメントはありません。
open/close この記事にコメントする



link もっと前
2014年1月26日 >>> 2014年1月26日
link もっと後

管理用メニュー

link 記事を新規作成

<2014>
<<<01>>>
---1234
567891011
12131415161718
19202122232425
262728293031-

最近のコメント5件

  • link 24年5月16日
    すずきさん (05/21 11:41)
    「あー、確かにdpkg-reconfigu...」
  • link 24年5月16日
    hdkさん (05/21 08:55)
    「システム全体のlocale設定はDebi...」
  • link 24年5月17日
    すずきさん (05/20 13:16)
    「そうですねえ、普通はStandardなの...」
  • link 24年5月17日
    hdkさん (05/19 07:45)
    「なるほど、そういうことなんですね。Exc...」
  • link 24年5月17日
    すずきさん (05/19 03:41)
    「Standardだと下記の設定になってい...」

最近の記事3件

  • link 24年5月19日
    すずき (06/04 00:44)
    「[Yocto - まとめリンク] 目次: YoctoHello YoctoYoctoのセットアップスクリプトとビルドディレクト...」
  • link 24年5月31日
    すずき (06/04 00:43)
    「[Bitbakeのクラス] 目次: YoctoYocto Scarthgap(5.0.1)のメモです。前回同様、コードを読んで...」
  • link 24年5月30日
    すずき (06/04 00:35)
    「[Bitbakeのレイヤー] 目次: YoctoYocto Scarthgap(5.0.1)のメモです。Yoctoの使い方は以...」
link もっとみる

こんてんつ

open/close wiki
open/close Linux JM
open/close Java API

過去の日記

open/close 2002年
open/close 2003年
open/close 2004年
open/close 2005年
open/close 2006年
open/close 2007年
open/close 2008年
open/close 2009年
open/close 2010年
open/close 2011年
open/close 2012年
open/close 2013年
open/close 2014年
open/close 2015年
open/close 2016年
open/close 2017年
open/close 2018年
open/close 2019年
open/close 2020年
open/close 2021年
open/close 2022年
open/close 2023年
open/close 2024年
open/close 過去日記について

その他の情報

open/close アクセス統計
open/close サーバ一覧
open/close サイトの情報

合計:  counter total
本日:  counter today

link About www.katsuster.net
RDFファイル RSS 1.0

最終更新: 06/04 00:44