アットウィキロゴ

Tutorial_8

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

Tutorial_8 - (2010/10/21 (木) 13:19:16) の1つ前との変更点

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

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

[[トップ>トップページ]] > [[チュートリアル和訳>Tutorial和訳]] > [[8 Genericity>Tutorial_8]] #co(){*8 Genericity The last characteristic of Scala we will explore in this tutorial is genericity. Java programmers should be well aware of the problems posed by the lack of genericity in their language, a shortcoming which is addressed in Java 1.5.} *8 ジェネリシティ このチュートリアルで調べる最後の Scala の特徴はジェネリシティです。Java プログラマは、Java にジェネリシティが無いことによる問題、Java 1.5 で検討される欠点、についてよく知っているに違いありません。 #co(){Genericity is the ability to write code parametrised by types. For example, a programmer writing a library for linked lists faces the problem of deciding which type to give to the elements of the list. Since this list is meant to be used in many different contexts, it is not possible to decide that the type of the elements has to be, say, Int. This would be completely arbitrary and overly restrictive.} ジェネリシティとは型でパラメータ化されたコードを書ける能力です。例えば、連結リストのライブラリを書こうとしたプログラマは、リストの要素にどの型を使うか決めるという問題に直面します。このリストは色々な場面で使うつもりですから、要素の型を例えば Int とか決めることは不可能です。それでは全く恣意的かつ制限が強すぎるでしょう。 #co(){Java programmers resort to using Object, which is the super-type of all objects. This solution is however far from being ideal, since it doesn’t work for basic types (int, long, float, etc.) and it implies that a lot of dynamic type casts have to be inserted by the programmer.} Java プログラマは、全てのオブジェクトのスーパータイプである Object の助けを借ります。しかしこの解決方法は理想とはほど遠いものです。なぜなら、基本型 (int, long, float など) には使えませんし、プログラマはたくさんの動的な型キャストを挿入しなければならないからです。 #co(){Scala makes it possible to define generic classes (and methods) to solve this problem. Let us examine this with an example of the simplest container class possible: a reference, which can either be empty or point to an object of some type.} Scala ではこの問題を解決するためにジェネリッククラス(とメソッド)を定義できます。もっとも簡単なコンテナクラスである参照の例を見てみましょう。参照は、空であるか何かの型のオブジェクトを指しています。 class Reference[T] { private var contents: T = _ def set(value: T) { contents = value } def get: T = contents } #co(){The class Reference is parametrised by a type, called T, which is the type of its element. This type is used in the body of the class as the type of the contents variable, the argument of the set method, and the return type of the get method.} Reference クラスはその要素の型である型 T でパラメータ化されます。この型はクラスの本体で、変数 content 、set メソッドの引数、get メソッドの戻り値、の型として使われます。 #co(){The above code sample introduces variables in Scala, which should not require further explanations. It is however interesting to see that the initial value given to that variable is _, which represents a default value. This default value is 0 for numeric types, false for the Boolean type, () for the Unit type and null for all object types.} 上記のサンプルコードで、Scala の変数が導入されていますが、これ以上の説明は不要でしょう。ただしその変数の初期値が _ として与えられていることは興味深いでしょう。_ はデフォルト値を表します。デフォルト値は、数値型に対しては 0 、Boolean 型に対しては &bold(){false}、Unit 型に対しては () 、全てのオブジェクト型に対しては &bold(){null} です。 #co(){To use this Reference class, one needs to specify which type to use for the type parameter T, that is the type of the element contained by the cell. For example, to create and use a cell holding an integer, one could write the following:} この Reference クラスを使うためには、型パラメータ T 、すなわち cell に格納される要素型を指定する必要があります。例えば、整数を格納する cell を作るには下記のようにします。 object IntegerReference { def main(args: Array[String]) { val cell = new Reference[Int] cell.set(13) println("Reference contains the half of " + (cell.get * 2)) } } #co(){As can be seen in that example, it is not necessary to cast the value returned by the get method before using it as an integer. It is also not possible to store anything but an integer in that particular cell, since it was declared as holding an integer.} この例で見たように get メソッドの戻り値を、整数として使う前に値をキャストする必要はありません。また、整数でない何かをその cell に代入することはできません。なぜなら整数を格納すると宣言されているからです。 #center(){[[前ページ>>Tutorial_7]]  [[目次>>Tutorial和訳]]  [[次ページ>>Tutorial_9]]} ---- #comment()
[[トップ>トップページ]] > [[チュートリアル和訳>Tutorial和訳]] > [[8 Genericity>Tutorial_8]] #co(){*8 Genericity The last characteristic of Scala we will explore in this tutorial is genericity. Java programmers should be well aware of the problems posed by the lack of genericity in their language, a shortcoming which is addressed in Java 1.5.} *8 ジェネリシティ このチュートリアルで調べる最後の Scala の特徴はジェネリシティです。Java プログラマは、Java にジェネリシティが無いことによる問題、Java 1.5 で検討される欠点、についてよく知っているに違いありません。 #co(){Genericity is the ability to write code parametrised by types. For example, a programmer writing a library for linked lists faces the problem of deciding which type to give to the elements of the list. Since this list is meant to be used in many different contexts, it is not possible to decide that the type of the elements has to be, say, Int. This would be completely arbitrary and overly restrictive.} ジェネリシティとは型でパラメータ化されたコードを書ける能力です。例えば、連結リストのライブラリを書こうとしたプログラマは、リストの要素にどの型を使うか決めるという問題に直面します。このリストは色々な場面で使うつもりですから、要素の型を例えば Int とか決めることは不可能です。それでは全く恣意的かつ制限が強すぎるでしょう。 #co(){Java programmers resort to using Object, which is the super-type of all objects. This solution is however far from being ideal, since it doesn’t work for basic types (int, long, float, etc.) and it implies that a lot of dynamic type casts have to be inserted by the programmer.} Java プログラマは、全てのオブジェクトのスーパータイプである Object の助けを借ります。しかしこの解決方法は理想とはほど遠いものです。なぜなら、基本型 (int, long, float など) には使えませんし、プログラマはたくさんの動的な型キャストを挿入しなければならないからです。 #co(){Scala makes it possible to define generic classes (and methods) to solve this problem. Let us examine this with an example of the simplest container class possible: a reference, which can either be empty or point to an object of some type.} Scala ではこの問題を解決するためにジェネリッククラス(とメソッド)を定義できます。もっとも簡単なコンテナクラスである参照の例を見てみましょう。参照は、空であるか何かの型のオブジェクトを指しています。 class Reference[T] { private var contents: T = _ def set(value: T) { contents = value } def get: T = contents } #co(){The class Reference is parametrised by a type, called T, which is the type of its element. This type is used in the body of the class as the type of the contents variable, the argument of the set method, and the return type of the get method.} Reference クラスはその要素の型である型 T でパラメータ化されます。この型はクラスの本体で、変数 content 、set メソッドの引数、get メソッドの戻り値、の型として使われます。 #co(){The above code sample introduces variables in Scala, which should not require further explanations. It is however interesting to see that the initial value given to that variable is _, which represents a default value. This default value is 0 for numeric types, false for the Boolean type, () for the Unit type and null for all object types.} 上記のサンプルコードで、Scala の変数が導入されていますが、これ以上の説明は不要でしょう。ただしその変数の初期値が _ として与えられていることは興味深いでしょう。_ はデフォルト値を表します。デフォルト値は、数値型に対しては 0 、Boolean 型に対しては &bold(){false}、Unit 型に対しては () 、全てのオブジェクト型に対しては &bold(){null} です。 #co(){To use this Reference class, one needs to specify which type to use for the type parameter T, that is the type of the element contained by the cell. For example, to create and use a cell holding an integer, one could write the following:} この Reference クラスを使うためには、型パラメータ T 、すなわち cell に格納される要素型を指定する必要があります。例えば、整数を格納する cell を作るには下記のようにします。 object IntegerReference { def main(args: Array[String]) { val cell = new Reference[Int] cell.set(13) println("Reference contains the half of " + (cell.get * 2)) } } #co(){As can be seen in that example, it is not necessary to cast the value returned by the get method before using it as an integer. It is also not possible to store anything but an integer in that particular cell, since it was declared as holding an integer.} この例で見たように get メソッドの戻り値を、整数として使う前に値をキャストする必要はありません。また、整数でない何かをその cell に代入することはできません。なぜなら整数を格納すると宣言されているからです。 #center(){[[前ページ>Tutorial_7]]  [[目次>Tutorial和訳]]  [[次ページ>Tutorial_9]]} ---- #comment()

表示オプション

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

下から選んでください:

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