Spec2.8Chap12a

第 12 章 Scala 標準ライブラリ (The Scala Standard Library)

Scala 標準ライブラリは、多数のクラスとモジュールを備えた scala パッケージからなります。以下で、それらのいくつかについて説明します。


12.1 ルートクラス (Root Classes)

Figure 12.1 illustrates Scala's class hierarchy. The root of this hierarchy is formed by class Any . Every class in a Scala execution environment inherits directly or indirectly from this class . Class Any has two direct subclasses: AnyRef and AnyVal .

図 12.1 は Scala のクラス階層を示しています。


imageプラグインエラー : 画像を取得できませんでした。しばらく時間を置いてから再度お試しください。

図 12.1 Scala のクラス階層

この階層構造のルートは、クラス Any です。 Scala 実行環境中のすべてのクラスは、このクラスを直接あるいは間接に継承しています。 クラス Any は、直接のサブクラスを 2 つ持っています: AnyRef と AnyVal です。

The subclass AnyRef represents all values which are represented as objects in the underlying host system . Every user-defined Scala class inherits directly or indirectly from this class . Furthermore, every user-defined Scala class also inherits the trait scala.ScalaObject . Classes written in other languages still inherit from scala.AnyRef, but not from scala.ScalaObject .

The class AnyVal has a fixed number of subclasses, which describe values which are not implemented as objects in the underlying host system .

サブクラス AnyRef は、 ホストシステム中でオブジェクトとして表されるすべての値を表現します。 ユーザーが定義するすべての Scala クラスは、 直接あるいは間接にこのクラスを継承します。 さらに、すべてのユーザー定義 Scala クラスは、トレイト scala.ScalaObject も継承します。 他の言語で書かれたクラスは、scala.ScalaObject ではなく、scala.AnyRef を継承します。

クラス AnyVal は、ホストシステム中でオブジェクトとして実装されない値を記述する、 固定数のサブクラスを持っています。

Classes AnyRef and AnyVal are required to provide only the members declared in class Any, but implementations may add host-specific methods to these classes (for instance, an implementation may identify class AnyRef with its own root class for objects) .

The signatures of these root classes are described by the following definitions .

クラス AnyRef と AnyVal は、 クラス Any 中で宣言されたメンバーだけを提供する必要がありますが、しかし実装では、 ホスト特有のメソッドをそれらクラスに加えることがあります (例えば、実装では、クラス AnyRef を自身のオブジェクトのルートクラスと同一視するかもしれません)。

これらルートクラスのシグニチャは、次の定義で記述されます。

   package scala
   /** The universal root class */
   /** 普遍的なルートクラス*/
   abstract class Any {
   
     /** equality定義; ここでは抽象*/
     def equals(that: Any): Boolean
   
       /** Semantic equality between values */
       /** 値の間のセマンティックな等価性 */
       final def == (that: Any): Boolean =
         if (null eq this) null eq that else this equals that
   
       /** Semantic inequality between values */
       /** 値の間の、セマンティックな非等価性 */
       final def != (that: Any): Boolean = !(this == that)
   
       /** Hash code; abstract here */
       /** ハッシュコード;ここでは抽象*/
       def hashCode: Int = ...
   
       /** Textual representation; abstract here */
       /** テキスト表現;ここでは抽象*/
       def toString: String = ...
   
       /** Type test; needs to be inlined to work as given */
       /** 型テスト;与動作にはインライン展開要 */
       def isInstanceOf[a]: Boolean
   
       /** Type cast; needs to be inlined to work as given */ */
       /** 型キャスト;与動作にはインライン展開要 */
       def asInstanceOf[A]: A = this match {
         case x: A => x
         case _ => if (this eq null) this
                   else throw new ClassCastException()
       }
   }
   
   /** The root class of all value types */
   /** すべての値型のルートクラス */
   
   final class AnyVal extends Any
   
   /** The root class of all reference types */
   /** 全ての参照型のルートクラス */
   class AnyRef extends Any {
     def equals(that: Any): Boolean      = this eq that
     final def eq(that: AnyRef): Boolean = ... // 参照等価性
     final def ne(that: AnyRef): Boolean = !(this eq that)
   
       def hashCode: Int = ...    // hashCode は割当てられたアドレスから計算
       def toString: String = ... // toString は hashCode とクラス名から計算
   
   /** A mixin class for every user-defined Scala class */
   /** すべてのユーザ定義 Scala クラスに対するミックスインクラス */
   trait ScalaObject extends AnyRef

型テスト x.isInstanceOf[T] は、次の型付きパターンマッチに等価です。

   x match {
     case _: T´ => true
     case _ => false
   }

where the type T´ is the same as T except if T is of the form D or D[tps] , where D is a type member of some outer class C . In this case T´ is C#D (or C#D[tps], respectively), whereas T itself would expand to C.this.D[tps]. In other words, an isInstanceOf test does not check for the

ここで 型 T´ は、T が D あるいは D[tps] の形 (D はある外側のクラス C の型メンバー) である場合を除き、T と同じ。 この場合 T´は C#D(あるいは、D[tps]に対しては C#D[tps])であるのに対して、 T 自身は C.this.D[tps] に拡張されます。 言い替えれば、isInstanceOf テストは...をチェックしません。

The test x.asInstanceOf[T] is treated specially if T is a numeric value type (§12.2). In this case the cast will be translated to an application of a conversion method x.toT (§12.2.1). For non-numeric values x the operation will raise a ClassCastException .

もし T が数値型(§12.2)なら、テスト x.asInstanceOf[T] は特別に扱われます。 この場合、キャストは変換メソッド x.toT(§12.2.1)の適用に翻訳されます。 非数値 x に対する操作は ClassCastException を引き起こします。



12.2 値クラス (Value Classes)

Value classes are classes whose instances are not represented as objects by the underlying host system . All value classes inherit from class AnyVal . Scala implementations need to provide the value classes Unit, Boolean, Double, Float, Long, Int, Char, Short, and Byte (but are free to provide others as well). The signatures of these classes are defined in the following .

値クラスは、 そのインスタンスがホストシステムではオブジェクトとして表されないクラスです。 すべての値クラスはクラス AnyVal を継承します。 Scala の実装は、値クラス Unit、Boolean、Double、Float、Long、Int、Char、 Short とByte を提供する必要があります (しかし、同様に他のものを供給するのは自由です)。 これらクラスのシグニチャは、以降で定義されています。



12.2.1 数値型 (Numeric Value Types)

Classes Double, Float, Long, Int, Char, Short, and Byte are together called numeric value types . Classes Byte, Short, or Char are called subrange types . Subrange types, as well as Int and Long are called integer types, whereas Float and Double are called floating point types .

Numeric value types are ranked in the following partial order:

クラス Double、Float、Long、Int、Char、Short と Byte はまとめて 数値型 と呼ばれます。 クラス Byte、Short、あるいは Char は、 部分領域型 と呼ばれます。 Int や Long と同様、部分領域型は 整数型 と呼ばれ、他方、Float と Double は 浮動小数点型 と 呼ばれます。

数値型は、次の半順序でランク付けされます:

      Byte - Short
                   \
                       Int - Long - Float - Double
                   /
              Char

Byte and Short are the lowest-ranked types in this order, whereas Double is the highest-ranked . Ranking does not imply a conformance (§3.5.2) relationship; for instance Int is not a subtype of Long . However, object Predef (§12.5) defines views (§7.3) from every numeric value type to all higher-ranked numeric value types . Therefore, lower-ranked types are implicitly converted to higher-ranked types when required by the context (§6.26) .

Byte と Short はこの順序中の最も低いランク型であるのに対して、 Double は最も高いランクです。 ランク付けは適合関係 (§3.5.2)を意味 しません 。; たとえば、インスタンス Int は Long のサブ型ではありません。 しかし、オブジェクト Predef (§12.5)では、すべての数値型から、 すべてのより高いランクの数値型へのビュー (§7.3)を定義しています。 ですから、コンテキストによって必要とされるとき、より低いランクの型は 暗黙のうちにより高いランクの型へ変換されます (§6.26)。

Given two numeric value types S and T , the operation type of S and T is defined as follows: If both S and T are subrange types then the operation type of S and T is Int . Otherwise the operation type of S and T is the larger of the two types wrt ranking .Given two numeric values v and w the operation type of v and w is the operation type of their run-time types .

与えられた 2 つの数値型 S と T に対し、S と T の 演算型 は 次のように定義されます。:もし S と T の両方が部分領域型なら、 S と T の演算型は Int です。 そうでなければ S と T の演算型は、2 つの型のうちランクに関してより高い方です。 与えられた 2 つの数値 v と w に対して、v と w の演算型は、 それらの実行時型の演算型です。

すべての数値型 T は、次のメソッドをサポートします。

  • Comparison methods for equals (==), not-equals (!=), less-than (<), greater-than (>), less-than-or-equals (<=), greater-than-or-equals (>=), which each exist in 7 overloaded alternatives. Each alternative takes a parameter of some numeric value type . Its result type is type Boolean . The operation is evaluated by converting the receiver and its argument to their operation type and performing the given comparison operation of that type .
  • 等しい (==)、等しくない(!=)、小さい (<)、大きい(>)、小さいか等しい (<=)、大きいか等しい (>=) 等のための比較メソッド。 それぞれに 7 つのオーバーロードされた代替物があります。 各代替物は、何らかの数値型パラメータをとります。 その結果型は Boolean 型です。 演算は、レシーバとその引数をそれらの演算型へ変換し、その型の与えられた比較演算を実行することで、評価されます。
  • Arithmetic methods addition (+), subtraction (-), multiplication (*), division (/), and remainder (%), which each exist in 7 overloaded alternatives . Each alternative takes a parameter of some numeric value type U . Its result type is the operation type of T and U . The operation is evaluated by converting the receiver and its argument to their operation type and performing the given arithmetic operation of that type .
  • 算術演算メソッドの、加算 (+)、減算 (-)、乗算 (*)、除算 (/) と 剰余 (%)。それぞれに 7 つのオーバーロードされた代替物があります。 各代替物は、何らかの数値型 U をもつパラメータをとります。 その結果型は T と U の演算型です。 演算は、レシーバとその引数をそれらの演算型へ変換し、その型の与えられた算術演算を実行することで、評価されます。
  • Parameterless arithmethic methods identity (+) and negation (-), with result type T . The first of these returns the receiver unchanged, whereas the second returns its negation .
  • パラメータなしの算術演算メソッド、同値 (+) と符号反転 (-)。結果型は T です。 1 つめのメソッドは変わらないレシーバを返すのに対し、2 つめはその符号反転を返します。
  • Conversion methods toByte, toShort, toChar, toInt, toLong, toFloat, toDouble which convert the receiver object to the target type, using the rules of Java's numeric type cast operation . The conversion might truncate the numeric value (as when going from Long to Int or from Int to Byte) or it might lose precision (as when going from Double to Float or when converting between Long and Float) .
  • 変換メソッドの、toByte、toShort、toChar、toInt、toLong、toFloat、toDouble。これらは、Java の数値型キャスト操作規則を使って、レシーバオブジェクトをターゲット型に変換します。 変換は数値を切り落とすかもしれません(Long から Int、あるいは Int から Byte へ変換する時)。あるいはまた、変換により精度が失われるかもしれません (Double から Float、あるいは Long と Float 間で変換するとき)。

整数値型は、さらに次の演算をサポートしています。:

  • Bit manipulation methods bitwise-and (&), bitwise-or |, and bitwiseexclusive -or (^), which each exist in 5 overloaded alternatives . Each alternative takes a parameter of some integer numeric value type . Its result type is the operation type of T and U . The operation is evaluated by converting the receiver and its argument to their operation type and performing the given bitwise operation of that type .
  • ビット操作メソッドの、ビット単位 AND (&)、ビット単位 OR (|)、ビット単位 XOR (^)。それぞれに 5 つのオーバーロードされた代替物があります。 各代替物はいくつかの整数値型パラメータをとります。 その結果型は T と U の演算型です。 演算は、レシーバとその引数をそれらの演算型へ変換し、その型の与えられたビット単位演算を実行することで、評価されます。
  • A parameterless bit-negation method (~). Its result type is the reciver type T or Int, whichever is larger . The operation is evaluated by converting the receiver to the result type and negating every bit in its value .
  • パラメータなしの、ビット反転メソッド(~)。 その結果型は、レシーバの型 T あるいは Int の、どちらか大きい方です。 演算は、レシーバをその結果型へ変換し、その値中の各ビットを反転させることで、評価されます。
  • Bit-shift methods left-shift (<<), arithmetic right-shift (>>), and unsigned right-shift (>>>). Each of these methods has two overloaded alternatives, which take a parameter n of type Int, respectively Long . The result type of the operation is the receiver type T , or Int, whichever is larger . The operation is evaluated by converting the receiver to the result type and performing the specified shift by n bits .
  • ビットシフトメソッドの、左シフト (<<)、算術右シフト (>>)、符号無し右シフト(>>>)。 これらの各メソッドは、それぞれ 2 つのオーバーロードされた代替物を持っていて、Int 型あるいは Long型のパラメータ n をとります。 演算の結果型は、レシーバの型 T あるいは Int の、どちらか大きい方です。 演算は、レシーバを結果型へ変換し、指定された n ビットシフトを実行することで、評価されます。

Numeric value types also implement operations equals, hashCode, and toString from class Any .

The equals method tests whether the argument is a numeric value type . If this is true, it will perform the == operation which is appropriate for that type . That is, the equals method of a numeric value type can be thought of being defined as follows:

数値型は、クラス Any の操作 equals、hashCode、toString も実装しています。

equals メソッドは、引数が数値型であるかどうかテストします。 もしそれが真なら、その型に適した == 演算を実行します。 すなわち、数値型の equals メソッドは、次のように定義されていると考えられます。

   def equals(other: Any): Boolean = other match {
     case that: Byte   => this == that
     case that: Short  => this == that
     case that: Char   => this == that
     case that: Int    => this == that
     case that: Long   => this == that
     case that: Float  => this == that
     case that: Double => this == that
     case _ => false
   }

The hashCode method returns an integer hashcode that maps equal numeric values to equal results . It is guaranteed to be the identity for for type Int and for all subrange types .

The toString method displays its receiver as an integer or floating point number .

hashCode メソッドは、等しい数値を等しい結果にマップする整数 hashcode を返します。 それは、Int 型とすべての部分領域型に対して同一性(identity)を保証します。

toString メソッドは、そのレシーバを整数あるいは浮動小数点数として表示します。


Example 12.2.1 例として、数値型 Int のシグニチャを示します。

   package scala
   abstract sealed class Int extends AnyVal {
       def == (that: Double): Boolean // double equality
       def == (that: Float): Boolean   // float equality
       def == (that: Long): Boolean    // long equality
       def == (that: Int): Boolean     // int equality
       def == (that: Short): Boolean   // int equality
       def == (that: Byte): Boolean    // int equality
       def == (that: Char): Boolean    // int equality
       /* analogous for !=, <, >, <=, >= */
   
       def + (that:   Double): Double     //   double addition
       def + (that:   Float): Double      //   float addition
       def + (that:   Long): Long         //   long addition
       def + (that:   Int): Int           //   int addition
       def + (that:   Short): Int         //   int addition
       def + (that:   Byte): Int          //   int addition
       def + (that:   Char): Int          //   int addition
       /* analogous   for -, *, /, % */
   
       def & (that:   Long): Long         //   long bitwise and
       def & (that:   Int): Int           //   int bitwise and
       def & (that:   Short): Int         //   int bitwise and
       def & (that:   Byte): Int          //   int bitwise and
       def & (that:   Char): Int          //   int bitwise and
       /* analogous   for |, ^ */
   
       def << (cnt: Int): Int             // int left shift
       def << (cnt: Long): Int            // long left shift
       /* analogous for >>, >>> */
   
       def unary_+ : Int                  // int identity
       def unary_- : Int                  // int negation
       def unary_~ : Int                  // int bitwise negation
   
       def   toByte: Byte                 //   convert   to   Byte
       def   toShort: Short               //   convert   to   Short
       def   toChar: Char                 //   convert   to   Char
       def   toInt: Int                   //   convert   to   Int
       def   toLong: Long                 //   convert   to   Long
       def   toFloat: Float               //   convert   to   Float
       def   toDouble: Double             //   convert   to   Double
   }



12.2.2 クラス Boolean (Class Boolean)

Class Boolean has only two values: true and false . It implements operations as given in the following class definition .

クラス Boolean は、ただ 2 つの値を持っています。: true と false です。 それは、次のクラス定義で与えられるのと同等の演算を実装しています。

   package scala
   abstract sealed class Boolean extends AnyVal {
     def && (p: => Boolean): Boolean =   // boolean and
       if (this) p else false
     def || (p: => Boolean): Boolean =   // boolean or
       if (this) true else p
     def & (x: Boolean): Boolean =       // boolean strict and
       if (this) x else false
     def | (x: Boolean): Boolean =       // boolean strict or
       if (this) true else x
     def == (x: Boolean): Boolean =      // boolean equality
       if (this) x else x.unary_!
     def != (x: Boolean): Boolean        // boolean inequality
       if (this) x.unary_! else x
     def unary_!: Boolean                // boolean negation
       if (this) false else true
   }

The class also implements operations equals, hashCode, and toString from class Any . The equals method returns true if the argument is the same boolean value as the receiver, false otherwise . The hashCode method returns a fixed, implementationspecific hash-code when invoked on true, and a different, fixed, implementationspecific hash-code when invoked on false . The toString method returns the receiver converted to a string, i.e. either "true" or "false" .

このクラスはクラス Any の操作 equals、hashCode と toString も実装します。

equals メソッドは、もし引数がレシーバと同じ boolean 値なら true を返し、そうでなければ false を返します。 hashCode メソッドは、true上で起動されたときには、 固定された、実装固有のハッシュコードを返し、false上で起動されたときには、 異なる、固定された、実装固有のハッシュコードを返します。 toString メソッドは、文字列に変換されたレシーバを返します、すなわち、"true" あるいは "false" のいずれかを返します。



12.2.3 クラス Unit (Class Unit)

Class Unit has only one value: (). It implements only the three methods equals, hashCode, and toString from class Any.

The equals method returns true if the argument is the unit value (), false otherwise . The hashCode method returns a fixed, implementation-specific hash-code, The toString method returns "()" .

クラス Unit は、ただ 1 つの値を持っています。: ()です。 これは、クラス Any の equals、hashCode、と toStringt のただ 3 つのメソッドを実装しています。

equals メソッドは、もし引数が unit 値 () なら true を返し、 そうでなければ false を返します。 hashCode メソッドは、固定された、実装固有のハッシュコードを返します。 toString メソッドは、"()" を返します。

タグ:

+ タグ編集
  • タグ:

このサイトはreCAPTCHAによって保護されており、Googleの プライバシーポリシー利用規約 が適用されます。

最終更新:2011年03月07日 08:54
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。