Scala 標準ライブラリは、多数のクラスとモジュールを備えた scala パッケージからなります。以下で、それらのいくつかについて説明します。
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 のクラス階層を示しています。
図 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 を引き起こします。
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 を提供する必要があります (しかし、同様に他のものを供給するのは自由です)。 これらクラスのシグニチャは、以降で定義されています。
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 は、次のメソッドをサポートします。
整数値型は、さらに次の演算をサポートしています。:
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 }
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" のいずれかを返します。
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 メソッドは、"()" を返します。