「Spec2.8Chap9a」の編集履歴(バックアップ)一覧はこちら
Spec2.8Chap9a - (2011/02/28 (月) 15:14:54) の1つ前との変更点
追加された行は緑色になります。
削除された行は赤色になります。
#setmenu2(spec2.8-r-menu)
* 第 9 章 トップレベル定義 (Top-Level Definitions)
&br()
&br()
&aname(9.1,option=nolink){ }
** 9.1 コンパイル単位 (Compilation Units)
&bold(){構文:}
CompilationUnit ::= {'package' QualId semi} TopStatSeq
TopStatSeq ::= TopStat {semi TopStat}
TopStat ::= {Annotation} {Modifier} TmplDef
| Import
| Packaging
| PackageObject
|
QualId ::= id {'.' id}
A compilation unit consists of a sequence of packagings, import clauses, and class and object definitions, which may be preceded by a package clause .
コンパイル単位は、パッケージング、インポート節、クラスとオブジェクト定義、
等の並びからなり、先行してパッケージ節があっても構いません。
次の、1 つ以上のパッケージ節で始まるコンパイル単位
package p1 ;
...
package pn ;
stats
starting with one or more package clauses is equivalent to a compilation unit consisting of the packaging
は、次のパッケージングからなるコンパイル単位に等価です。
package p1 { ...
package pn {
stats
} ...
}
Implicitly imported into every compilation unit are, in that order : the package java.lang, the package scala, and the object scala.Predef (§12.5).
Members of a later import in that order hide members of an earlier import .
すべてのコンパイル単位中に暗黙のうちにインポートされるものは、順に、
パッケージ java.lang、パッケージ scala とオブジェクト scala.Predef
(&link_anchor(12.5,page=Spec2.8Chap12a){§12.5}) です。
この順番の中で、後でインポートされたメンバーは、
前でインポートされたメンバーを隠します。
&br()
&br()
&aname(9.2,option=nolink){ }
** 9.2 パッケージング (Packagings)
&bold(){構文:}
Packaging ::= 'package' QualId [nl] '{' TopStatSeq '}'
A package is a special object which defines a set of member classes, objects and packages .
Unlike other objects, packages are not introduced by a definition .
Instead , the set of members of a package is determined by packagings .
パッケージはメンバークラス、
オブジェクトとパッケージの集合を定義する特別なオブジェクトです。
他のオブジェクトと異なり、パッケージは定義では導入されません。
その代わりに、パッケージのメンバーの集合は、パッケージングによって決定されます。
A packaging package p { ds } injects all definitions in ds as members into the package whose qualified name is p .
Members of a package are called top-level definitions .
If a definition in ds is labeled private, it is visible only for other members in the package .
パッケージング package p { ds } は、
ds 中のすべての定義をその限定修飾された名前が p であるパッケージに、
メンバーとして注入します。
パッケージのメンバーは&strong(){トップレベル}定義と呼ばれます。
もし ds 中の定義が private と印されていれば、
そのパッケージ中の他のメンバーからのみ可視となります。
Inside the packaging, all members of package p are visible under their simple names .
However this rule does not extend to members of enclosing packages of p that are designated by a prefix of the path p .
パッケージング内では、パッケージ p のすべてのメンバーについて、
それらの単純名が可視となります。
しかし、この規則は、パス p の前置子によって指定される、
p が取り囲むパッケージのメンバーには拡張されません。
&br()
&bold(){Example 9.2.1}
次のパッケージングが与えられているとします。
package org.net.prj {
...
}
all members of package org.net.prj are visible under their simple names, but members of packages org or org.net require explicit qualification or imports .
パッケージ org.net.prj のすべてのメンバーは、その単純名が可視です。
しかしパッケージ org あるいは org.net のメンバーは、
明示的な限定修飾あるいはインポートを必要とします。
Selections p.m from p as well as imports from p work as for objects .
However, unlike other objects, packages may not be used as values .
It is illegal to have a package with the same fully qualified name as a module or a class .
p からの選択 p.m は、p からのインポートと同様、
オブジェクトについてうまく機能します。
しかし、他のオブジェクトと異なり、パッケージは値として使用できません。
モジュールあるいはクラスと完全修飾された同じ名前のパッケージは不正です。
Top-level definitions outside a packaging are assumed to be injected into a special empty package .
That package cannot be named and therefore cannot be imported .
However, members of the empty package are visible to each other without qualification .
パッケージング外でのトップレベル定義は、
特別な空パッケージに注入されるとみなされます。
このパッケージは、名前を付けることはできず、したがってインポートできません。
しかし、空パッケージのメンバーは互いに限定修飾なしで可視です。
&br()
&br()
&aname(9.3,option=nolink){ }
** 9.3 パッケージオブジェクト (Package Objects)
&bold(){構文:}
PackageObject ::= 'package' 'object' ObjectDef
A package object package object p extends t adds the members of template t to the package p .
There can be only one package object per package .
The standard naming convention is to place the definition above in a file named package.scala that's located in the directory corresponding to package p .
パッケージオブジェクト package object p extends t は、
テンプレート t のメンバーをパッケージ p に加えます。
パッケージ毎に、ただ 1 つのパッケージオブジェクトが可能です。
標準的な命名規則では、上記の定義を、
パッケージ p に直接対応するディレクトリ中の package.scala
という名前のファイルに置きます。
The package object should not define a member with the same name as one of the top-level objects or classes defined in package p .
If there is a name conflict, the behavior of the program is currently undefined .
It is expected that this restriction will be lifted in a future version of Scala .
パッケージオブジェクトは、パッケージ p 中で定義されたトップレベルオブジェクト
あるいはクラスの 1 つと同じ名前のメンバーを定義すべきではありません。
名前の衝突がある場合、プログラムの振る舞いは現在未定義です。
この制限は Scala の将来のバージョンで取り上げられることが期待されます。
&br()
&br()
&aname(9.4,option=nolink){ }
** 9.4 パッケージ参照 (Package References)
&bold(){構文:}
QualId ::= id {'.' id}
A reference to a package takes the form of a qualified identifier .
Like all other references , package references are relative .
That is, a package reference starting in a name p will be looked up in the closest enclosing scope that defines a member named p .
The special predefined name _root_ refers to the outermost root package which contains all top-level packages .
パッケージへの参照は、限定修飾された識別子の形をとります。
他のすべての参照と同じように、パッケージ参照は相対的です。
すなわち、名前 p で始まるパッケージ参照は、
名前 p のメンバーを定義する、最も近く取り囲むスコープが検索されます。
事前定義された特別な名前 _root_ は、すべてのトップレベルのパッケージを含む、
最も外側のルートパッケージを参照します。
&br()
&bold(){Example 9.4.1} 次のプログラムを考えます。:
package b {
class B
}
package a.b {
class A {
val x = new _root_.b.B
}
}
Here, the reference _root_.b.B refers to class B in the toplevel package b .
If the _root_ prefix had been omitted, the name b would instead resolve to the package a.b, and, provided that package does not also contain a class B, a compiler-time error would result .
ここで、参照 _root_.b.B は、トップレベルパッケージ b 中のクラス B
を参照します。
もし _root_ 前置子が省略されたなら、名前 b は代わりに、パッケージ a.b
へ変換されます。そしてそのパッケージもクラス B を含まないなら、
コンパイルエラーとなります。
&br()
&br()
&aname(9.5,option=nolink){ }
** 9.5 プログラム (Programs)
A program is a top-level object that has a member method main of type (Array[String])Unit .
Programs can be executed from a command shell.
The program's command arguments are are passed to the main method as a parameter of type Array[String] .
&strong(){プログラム}は、型 (Array[String])Unit のメンバーメソッド main
を持つトップレベルのオブジェクトです。
プログラムはコマンドシェルから実行できます。
プログラムのコマンド引数は、型 Array[String] のパラメータとして main
メソッドへ渡されます。
The main method of a program can be directly defined in the object, or it can be inherited .
The scala library defines a class scala.Application that defines an empty inherited main method .
An objects m inheriting from this class is thus a program, which executes the initializaton code of the object m .
プログラムの main メソッドは、オブジェクト中で直接定義するか、あるいは、
それを継承できます。
Scala ライブラリでは、空の継承された main メソッドを定義する、クラス
scala.Application を定義しています。
このクラスを継承するオブジェクト m は 1 つのプログラムとなり、
オブジェクト m の 初期化コードを実行します。
&br()
&bold(){Example 9.5.1}
The following example will create a hello world program by defining a method main in module test.HelloWorld .
次の例は、モジュール test.HelloWorld の中でメソッド main を定義し、
hello world プログラムを生成します。
package test
object HelloWord {
def main(args: Array[String]) { println("hello world") }
}
このプログラムはコマンドで起動できます。
scala test.HelloWorld
Java 環境では、次のコマンド
java test.HelloWorld
は、同様にうまく働くでしょう。
HelloWorld は main メソッドがなくても、
その代わりに Application を継承することで同様に定義できます。
package test
object HelloWord extends Application {
println("hello world")
}
#center(){[[前:8章>Spec2.8Chap8b]] [[目次>Spec2.8和訳]] [[次:10章>Spec2.8Chap10a]]}
#setmenu2(spec2.8-r-menu)
* 第 9 章 トップレベル定義 (Top-Level Definitions)
&br()
&br()
&aname(9.1,option=nolink){ }
** 9.1 コンパイル単位 (Compilation Units)
&bold(){構文:}
CompilationUnit ::= {'package' QualId semi} TopStatSeq
TopStatSeq ::= TopStat {semi TopStat}
TopStat ::= {Annotation} {Modifier} TmplDef
| Import
| Packaging
| PackageObject
|
QualId ::= id {'.' id}
A compilation unit consists of a sequence of packagings, import clauses, and class and object definitions, which may be preceded by a package clause .
コンパイル単位は、パッケージング、インポート節、クラスとオブジェクト定義、
等の並びからなり、先行してパッケージ節があっても構いません。
次の、1 つ以上のパッケージ節で始まるコンパイル単位
package p1 ;
...
package pn ;
stats
starting with one or more package clauses is equivalent to a compilation unit consisting of the packaging
は、次のパッケージングからなるコンパイル単位に等価です。
package p1 { ...
package pn {
stats
} ...
}
Implicitly imported into every compilation unit are, in that order : the package java.lang, the package scala, and the object scala.Predef (§12.5).
Members of a later import in that order hide members of an earlier import .
すべてのコンパイル単位中に暗黙のうちにインポートされるものは、順に、
パッケージ java.lang、パッケージ scala とオブジェクト scala.Predef
(&link_anchor(12.5,page=Spec2.8Chap12a){§12.5}) です。
この順番の中で、後でインポートしたメンバーは、
前にインポートしたメンバーを隠します。
&br()
&br()
&aname(9.2,option=nolink){ }
** 9.2 パッケージング (Packagings)
&bold(){構文:}
Packaging ::= 'package' QualId [nl] '{' TopStatSeq '}'
A package is a special object which defines a set of member classes, objects and packages .
Unlike other objects, packages are not introduced by a definition .
Instead , the set of members of a package is determined by packagings .
パッケージはメンバークラス、
オブジェクトとパッケージの集合を定義する特別なオブジェクトです。
他のオブジェクトと異なり、パッケージは定義では導入されません。
その代わりに、パッケージのメンバーの集合は、パッケージングによって決定されます。
A packaging package p { ds } injects all definitions in ds as members into the package whose qualified name is p .
Members of a package are called top-level definitions .
If a definition in ds is labeled private, it is visible only for other members in the package .
パッケージング package p { ds } は、
ds 中のすべての定義をその限定修飾された名前が p であるパッケージに、
メンバーとして注入します。
パッケージのメンバーは&strong(){トップレベル}定義と呼ばれます。
もし ds 中の定義が private と印されていれば、
そのパッケージ中の他のメンバーからのみ可視となります。
Inside the packaging, all members of package p are visible under their simple names .
However this rule does not extend to members of enclosing packages of p that are designated by a prefix of the path p .
パッケージング内では、パッケージ p のすべてのメンバーについて、
それらの単純名が可視となります。
しかし、この規則は、パス p の前置子によって指定される、
p が取り囲むパッケージのメンバーには拡張されません。
&br()
&bold(){Example 9.2.1}
次のパッケージングが与えられているとします。
package org.net.prj {
...
}
all members of package org.net.prj are visible under their simple names, but members of packages org or org.net require explicit qualification or imports .
パッケージ org.net.prj のすべてのメンバーは、その単純名が可視です。
しかしパッケージ org あるいは org.net のメンバーは、
明示的な限定修飾あるいはインポートを必要とします。
Selections p.m from p as well as imports from p work as for objects .
However, unlike other objects, packages may not be used as values .
It is illegal to have a package with the same fully qualified name as a module or a class .
p からの選択 p.m は、p からのインポートと同様、
オブジェクトについてうまく機能します。
しかし、他のオブジェクトと異なり、パッケージは値として使用できません。
モジュールあるいはクラスと完全修飾された同じ名前のパッケージは不正です。
Top-level definitions outside a packaging are assumed to be injected into a special empty package .
That package cannot be named and therefore cannot be imported .
However, members of the empty package are visible to each other without qualification .
パッケージング外でのトップレベル定義は、
特別な空パッケージに注入されるとみなされます。
このパッケージは、名前を付けることはできず、したがってインポートできません。
しかし、空パッケージのメンバーは互いに限定修飾なしで可視です。
&br()
&br()
&aname(9.3,option=nolink){ }
** 9.3 パッケージオブジェクト (Package Objects)
&bold(){構文:}
PackageObject ::= 'package' 'object' ObjectDef
A package object package object p extends t adds the members of template t to the package p .
There can be only one package object per package .
The standard naming convention is to place the definition above in a file named package.scala that's located in the directory corresponding to package p .
パッケージオブジェクト package object p extends t は、
テンプレート t のメンバーをパッケージ p に加えます。
パッケージ毎に、ただ 1 つのパッケージオブジェクトが可能です。
標準的な命名規則では、上記の定義を、
パッケージ p に直接対応するディレクトリ中の package.scala
という名前のファイルに置きます。
The package object should not define a member with the same name as one of the top-level objects or classes defined in package p .
If there is a name conflict, the behavior of the program is currently undefined .
It is expected that this restriction will be lifted in a future version of Scala .
パッケージオブジェクトは、パッケージ p 中で定義されたトップレベルオブジェクト
あるいはクラスの 1 つと同じ名前のメンバーを定義すべきではありません。
名前の衝突がある場合、プログラムの振る舞いは現在未定義です。
この制限は Scala の将来のバージョンで取り上げられることが期待されます。
&br()
&br()
&aname(9.4,option=nolink){ }
** 9.4 パッケージ参照 (Package References)
&bold(){構文:}
QualId ::= id {'.' id}
A reference to a package takes the form of a qualified identifier .
Like all other references , package references are relative .
That is, a package reference starting in a name p will be looked up in the closest enclosing scope that defines a member named p .
The special predefined name _root_ refers to the outermost root package which contains all top-level packages .
パッケージへの参照は、限定修飾された識別子の形をとります。
他のすべての参照と同じように、パッケージ参照は相対的です。
すなわち、名前 p で始まるパッケージ参照は、
名前 p のメンバーを定義する、最も近く取り囲むスコープが検索されます。
事前定義された特別な名前 _root_ は、すべてのトップレベルのパッケージを含む、
最も外側のルートパッケージを参照します。
&br()
&bold(){Example 9.4.1} 次のプログラムを考えます。:
package b {
class B
}
package a.b {
class A {
val x = new _root_.b.B
}
}
Here, the reference _root_.b.B refers to class B in the toplevel package b .
If the _root_ prefix had been omitted, the name b would instead resolve to the package a.b, and, provided that package does not also contain a class B, a compiler-time error would result .
ここで、参照 _root_.b.B は、トップレベルパッケージ b 中のクラス B
を参照します。
もし _root_ 前置子が省略されたなら、名前 b は代わりに、パッケージ a.b
へ変換されます。そしてそのパッケージもクラス B を含まないなら、
コンパイルエラーとなります。
&br()
&br()
&aname(9.5,option=nolink){ }
** 9.5 プログラム (Programs)
A program is a top-level object that has a member method main of type (Array[String])Unit .
Programs can be executed from a command shell.
The program's command arguments are are passed to the main method as a parameter of type Array[String] .
&strong(){プログラム}は、型 (Array[String])Unit のメンバーメソッド main
を持つトップレベルのオブジェクトです。
プログラムはコマンドシェルから実行できます。
プログラムのコマンド引数は、型 Array[String] のパラメータとして main
メソッドへ渡されます。
The main method of a program can be directly defined in the object, or it can be inherited .
The scala library defines a class scala.Application that defines an empty inherited main method .
An objects m inheriting from this class is thus a program, which executes the initializaton code of the object m .
プログラムの main メソッドは、オブジェクト中で直接定義するか、あるいは、
それを継承できます。
Scala ライブラリでは、空の継承された main メソッドを定義する、クラス
scala.Application を定義しています。
このクラスを継承するオブジェクト m は 1 つのプログラムとなり、
オブジェクト m の 初期化コードを実行します。
&br()
&bold(){Example 9.5.1}
The following example will create a hello world program by defining a method main in module test.HelloWorld .
次の例は、モジュール test.HelloWorld の中でメソッド main を定義し、
hello world プログラムを生成します。
package test
object HelloWord {
def main(args: Array[String]) { println("hello world") }
}
このプログラムはコマンドで起動できます。
scala test.HelloWorld
Java 環境では、次のコマンド
java test.HelloWorld
は、同様にうまく働くでしょう。
HelloWorld は main メソッドがなくても、
その代わりに Application を継承することで同様に定義できます。
package test
object HelloWord extends Application {
println("hello world")
}
#center(){[[前:8章>Spec2.8Chap8b]] [[目次>Spec2.8和訳]] [[次:10章>Spec2.8Chap10a]]}