アットウィキロゴ

Example8.4

「Example8.4」の編集履歴(バックアップ)一覧はこちら

Example8.4 - (2011/02/24 (木) 08:43:35) の1つ前との変更点

追加された行は緑色になります。

削除された行は赤色になります。

#co(){ 8.4 Least Types Scala does not allow one to parameterize objects with types. That's why we originally defined a generic class EmptyStack[A], even though a single value denoting empty stacks of arbitrary type would do. For co-variant stacks, however, one can use the following idiom: } ** 8.4 最下層の型 (Least Types) Scala では、オブジェクトを型でパラメータ化することはできません。そういう訳で、たとえ任意の型の空スタックを表すただ一つの値で充分だったとしても、最初にジェネリッククラス EmptyStack[A] を定義しました。しかし、共変的スタックについては、次のイディオムを使えます。 object EmptyStack extends Stack[Nothing] { ... } #co(){ The bottom type Nothing contains no value, so the type Stack[Nothing] expresses the fact that an EmptyStack contains no elements. Furthermore, Nothing is a subtype of all other types. Hence, for co-variant stacks, Stack[Nothing] is a subtype of Stack[T], for any other type T. This makes it possible to use a single empty stack object in user code. For instance: } ボトム型 Nothing は値を持ちません。したがって型 Stack[Nothing] は、EmptyStack が要素を含まないことを表現します。さらに、Nothing は他のすべての型のサブタイプです。ですから、共変的スタックではどんな型 T に対しても、Stack[Nothing] は Stack[T] のサブタイプです。これによってユーザーコード中で、単一の空スタックオブジェクトを使用できます。たとえば、 val s = EmptyStack.push("abc").push(new AnyRef()) #co(){ Let's analyze the type assignment for this expression in detail. The EmptyStack object is of type Stack[Nothing], which has a method } この式について、型の割り当てを詳細に解析しましょう。EmpthStack オブジェクトは型 Stack[Nothing] であり、次のメソッドを持っています。 push[B >: Nothing](elem: B): Stack[B] . #co(){ Local type inference will determine that the type parameter B should be instantiated to String in the application EmptyStack.push("abc"). The result type of that application is hence Stack[String], which in turn has a method } 局所的な型推論は、型パラメータ B は EmptyStack の適用において String へとインスタンス化されているに違いない、と決定します。したがって、その適用の結果型(戻り値型)は Stack[String] であり、次のメソッドを持っています。 push[B >: String](elem: B): Stack[B] . #co(){ The final part of the value definition above is the application of this method to new AnyRef(). Local type inference will determine that the type parameter b should this time be instantiated to AnyRef, with result type Stack[AnyRef]. Hence, the type assigned to value s is Stack[AnyRef]. } 上記の値定義の最後の箇所は、このメソッドの new AnyRef() への適用です。局所的型推論は今回、型パラメータ B は AnyRef へとインスタンス化されているに違いなく、結果型は Stack[AnyRef] である、と決定します。したがって値 s に割り当てられる型は Stack[AnyRef] です。 #co(){ Besides Nothing, which is a subtype of every other type, there is also the type Null, which is a subtype of scala.AnyRef, and every class derived from it. The null literal in Scala is the only value of that type. This makes null compatible with every reference type, but not with a value type such as Int. } Nothing はすべての型のサブタイプでした。ほかに、型 Null というものもあり、これは scala.AnyRef と、AnyRefを継承するすべてのクラスのサブタイプです。 Scala の null リテラルはその型の唯一の値です。これによって null はすべての参照型と互換ですが、Int のような値型とは互換ではありません。 #co(){ We conclude this section with the complete improved definition of stacks. Stacks have now co-variant subtyping, the push method has been generalized, and the empty stack is represented by a single object. } 完全に改良したスタック定義で、この章を締めくくります。いまやスタックは共変的サブタイプで、push メソッドは一般化され、空スタックは単一のオブジェクトで表現されます。 abstract class Stack[+A] { def push[B >: A](x: B): Stack[B] = new NonEmptyStack(x, this) def isEmpty: Boolean def top: A def pop: Stack[A] } object EmptyStack extends Stack[Nothing] { def isEmpty = true def top = error("EmptyStack.top") def pop = error("EmptyStack.pop") } class NonEmptyStack[+A](elem: A, rest: Stack[A]) extends Stack[A] { def isEmpty = false def top = elem def pop = rest } #co(){ Many classes in the Scala library are generic. We now present two commonly used families of generic classes, tuples and functions. The discussion of another common class, lists, is deferred to the next chapter. } Scala ライブラリ内の多くのクラスはジェネリックです。ジェネリックなクラスの系統でよく使われる、タプルと関数を紹介します。他のよく使われるクラスであるリストは、次の章に回します。 #center(){[[前ページ>Example8.3]] [[ 8 章>Chapter 8 Generic Types and Methods]] [[目次>ScalaByExample和訳]] [[次ページ>Example8.5]]} ---- #comment
#co(){ 8.4 Least Types Scala does not allow one to parameterize objects with types. That's why we originally defined a generic class EmptyStack[A], even though a single value denoting empty stacks of arbitrary type would do. For co-variant stacks, however, one can use the following idiom: } #setmenu2(ex-r-menu) ** 8.4 最下層の型 (Least Types) Scala では、オブジェクトを型でパラメータ化することはできません。そういう訳で、たとえ任意の型の空スタックを表すただ一つの値で充分だったとしても、最初にジェネリッククラス EmptyStack[A] を定義しました。しかし、共変的スタックについては、次のイディオムを使えます。 object EmptyStack extends Stack[Nothing] { ... } #co(){ The bottom type Nothing contains no value, so the type Stack[Nothing] expresses the fact that an EmptyStack contains no elements. Furthermore, Nothing is a subtype of all other types. Hence, for co-variant stacks, Stack[Nothing] is a subtype of Stack[T], for any other type T. This makes it possible to use a single empty stack object in user code. For instance: } ボトム型 Nothing は値を持ちません。したがって型 Stack[Nothing] は、EmptyStack が要素を含まないことを表現します。さらに、Nothing は他のすべての型のサブタイプです。ですから、共変的スタックではどんな型 T に対しても、Stack[Nothing] は Stack[T] のサブタイプです。これによってユーザーコード中で、単一の空スタックオブジェクトを使用できます。たとえば、 val s = EmptyStack.push("abc").push(new AnyRef()) #co(){ Let's analyze the type assignment for this expression in detail. The EmptyStack object is of type Stack[Nothing], which has a method } この式について、型の割り当てを詳細に解析しましょう。EmpthStack オブジェクトは型 Stack[Nothing] であり、次のメソッドを持っています。 push[B >: Nothing](elem: B): Stack[B] . #co(){ Local type inference will determine that the type parameter B should be instantiated to String in the application EmptyStack.push("abc"). The result type of that application is hence Stack[String], which in turn has a method } 局所的な型推論は、型パラメータ B は EmptyStack の適用において String へとインスタンス化されているに違いない、と決定します。したがって、その適用の結果型(戻り値型)は Stack[String] であり、次のメソッドを持っています。 push[B >: String](elem: B): Stack[B] . #co(){ The final part of the value definition above is the application of this method to new AnyRef(). Local type inference will determine that the type parameter b should this time be instantiated to AnyRef, with result type Stack[AnyRef]. Hence, the type assigned to value s is Stack[AnyRef]. } 上記の値定義の最後の箇所は、このメソッドの new AnyRef() への適用です。局所的型推論は今回、型パラメータ b は AnyRef へとインスタンス化されているに違いなく、結果型は Stack[AnyRef] である、と決定します。したがって値 s に割り当てられる型は Stack[AnyRef] です。 #co(){ Besides Nothing, which is a subtype of every other type, there is also the type Null, which is a subtype of scala.AnyRef, and every class derived from it. The null literal in Scala is the only value of that type. This makes null compatible with every reference type, but not with a value type such as Int. } Nothing はすべての型のサブタイプでした。ほかに、型 Null というものもあり、これは scala.AnyRef と、AnyRefを継承するすべてのクラスのサブタイプです。 Scala の null リテラルはその型の唯一の値です。これによって null はすべての参照型と互換ですが、Int のような値型とは互換ではありません。 #co(){ We conclude this section with the complete improved definition of stacks. Stacks have now co-variant subtyping, the push method has been generalized, and the empty stack is represented by a single object. } 完全に改良したスタック定義で、この章を締めくくります。いまやスタックは共変的サブタイプで、push メソッドは一般化され、空スタックは単一のオブジェクトで表現されます。 abstract class Stack[+A] { def push[B >: A](x: B): Stack[B] = new NonEmptyStack(x, this) def isEmpty: Boolean def top: A def pop: Stack[A] } object EmptyStack extends Stack[Nothing] { def isEmpty = true def top = error("EmptyStack.top") def pop = error("EmptyStack.pop") } class NonEmptyStack[+A](elem: A, rest: Stack[A]) extends Stack[A] { def isEmpty = false def top = elem def pop = rest } #co(){ Many classes in the Scala library are generic. We now present two commonly used families of generic classes, tuples and functions. The discussion of another common class, lists, is deferred to the next chapter. } Scala ライブラリ内の多くのクラスはジェネリックです。ジェネリックなクラスの系統でよく使われる、タプルと関数を紹介します。他のよく使われるクラスであるリストは、次の章に回します。 #center(){[[前ページ>Example8.3]] [[ 8 章>Chapter 8 Generic Types and Methods]] [[目次>ScalaByExample和訳]] [[次ページ>Example8.5]]} ---- #comment

表示オプション

横に並べて表示:
変化行の前後のみ表示:
ツールボックス

下から選んでください:

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