Spec2.8Chap2a

第 2 章 識別子・名前・スコープ (Identifiers, Names and Scopes)

Names in Scala identify types, values, methods, and classes which are collectively called entities. Names are introduced by local definitions and declarations (§4), inheritance (§5.1.3), import clauses (§4.7), or package clauses (§9.2) which are collectively called bindings.

Scala における名前は、ひとまとめに エンティティ と呼ばれる、 型、値、メソッド、そしてクラスを識別します。 名前はローカルな定義と宣言 (§4)、 継承(§5.1.3)、 インポート節(§4.7)、 あるいはパッケージ節(§9.2) によって導入され、それらはひとまとめに、 束縛(bindings) と呼ばれます。

Bindings of different kinds have a precedence defined on them:

  1. Definitions and declarations that are local, inherited, or made available by a package clause in the same compilation unit where the definition occurs have highest precedence.
  2. Explicit imports have next highest precedence.
  3. Wildcard imports have next highest precedence.
  4. Definitions made available by a package clause not in the compilation unit where the definition occurs have lowest precedence.

異なる種類の束縛は、それぞれ優先順位を持っています:

  1. 定義と宣言は、ローカルなもの、継承されたもの、あるいは定義が現れる同じコンパイル単位中のパッケージ節を通して利用可能となったものが、最も高い優先順位を持っています。
  2. 明示的なインポートが次に最も高い優先順位を持っています。
  3. ワイルドカードによるインポートが次に最も高い優先順位を持っています。
  4. 定義が現れるコンパイル単位中にないパッケージ節を通して利用可能となった定義が、最も低い優先順位を持っています。

There are two different name spaces, one for types (§3) and one for terms (§6). The same name may designate a type and a term, depending on the context where the name is used.

2 つの異なる名前空間があり、一つは型(types §3)のためのもの、もう一つは項 (terms §6)のためのものです。 その名前が使われるコンテキストによっては、 同じ名前で 1 つの型と 1 つの項を指定できます。

A binding has a scope in which the entity defined by a single name can be accessed using a simple name. Scopes are nested. A binding in some inner scope shadows bindings of lower precedence in the same scope as well as bindings of the same or lower precedence in outer scopes.

Note that shadowing is only a partial order. In a situation like

束縛は スコープ を持ち、そこでは、 ただ 1 つの名前によって定義されたエンティティは、 単純名を使ってアクセスできます。 スコープはネストされます。 内側のスコープ中の束縛は、 同じスコープ中のより低い優先順位の束縛を 隠します(shadow) 。 また、外側のスコープ中の、同じかあるいはより低い優先順位の束縛も隠します。

隠すことは、半順序関係にすぎないことに注意してください。 次の状況では

   val x = 1;   
   { import p.x;
     x }

x のいずれの束縛も、他を隠しません。 したがって、上記 3 行目の x への参照は曖昧です。

A reference to an unqualified (type- or term-) identifier x is bound by the unique binding, which

  • defines an entity with name x in the same namespace as the identifier, and
  • shadows all other bindings that define entities with name x in that namespace .

限定修飾されていない (型あるいは項の) 識別子 x への参照は、 ただ 1 つの束縛に結びつけられます。それは、

  • 識別子と同じ名前空間中で名前 x のエンティティを定義し、
  • その名前空間中で名前 x のエンティティを定義する、他のすべての束縛を隠します。

It is an error if no such binding exists. If x is bound by an import clause, then the simple name x is taken to be equivalent to the qualified name to which x is mapped by the import clause. If x is bound by a definition or declaration, then x refers to the entity introduced by that binding. In that case, the type of x is the type of the referenced entity.

もしそのような束縛が存在しないなら、エラーです。 もし x がインポート節によって束縛されるなら、単純名 x は、 インポート節によって x がマップされる、限定修飾された名前に等しいと解釈されます。 もし x が定義あるいは宣言によって束縛されるなら、 x はその束縛によって導入されたエンティティを参照します。 この場合、x の型は参照されるエンティティの型です。


Example 2.0.2 パッケージ P と Q 中に X という名前の 2 つのオブジェクト定義があるとします。

   package P {
     object X { val x = 1; val y = 2 }
   }
 
   package Q {
     object X { val x = true; val y = "" }      
   }

次のプログラムは、それらの間の異なる種類の束縛と優先順位を示します。

   package P {                   // 'X' パッケージ節による束縛
     import Console._            // 'println' ワイルドカード節による束縛
     object A {
       println("L4: "+X)         // ここでは、'X' は 'P.X' を参照
       object B {
         import Q._              // 'X' ワイルドカード節による束縛
         println("L7: "+X)       // ここでは 'X' は 'Q.X' を参照
         import X._              // 'x' と 'y' ワイルドカード節による束縛
         println("L8: "+x)       // ここでは 'x' は 'Q.X.x' を参照
         object C {
           val x = 3             // 'x' ローカル定義よって束縛
           println("L12: "+x)    // ここでは、'x' は定数 '3' を参照
           { import Q.X._        // 'x' と 'y' は、ワイルドカード節による束縛
   //        println("L14: "+x)  // ここでは 'x' への参照は曖昧
             import X.y          // 'y' 明示的インポートによる束縛
             println("L16: "+y)  // ここでは 'y' は 'Q.X.y' を参照
             { val x = "abc"     // 'x' ローカル定義よって束縛
              import P.X._       // 'x' と 'y' ワイルドカード節による束縛
   //         println("L19: "+y) // ここでは 'y' への参照は曖昧
              println("L20: "+x) // ここでは 'x' は文字列 ''abc''あ を参照
   }}}}}}

A reference to a qualified (type- or term-) identifier e.x refers to the member of the type T of e which has the name x in the same namespace as the identifier. It is an error if T is not a value type (§3.2). The type of e.x is the member type of the referenced entity in T .

限定修飾された(型あるいは項の)識別子 e.x への参照は、 識別子と同じ名前空間中の名前 x を持つ、e の型 T のメンバーを参照します。 もし T が値型(§3.2)でないなら、エラーです。 e.x の型は、T 中の参照されたエンティティのメンバー型です。

タグ:

+ タグ編集
  • タグ:

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

最終更新:2011年02月23日 18:28
ツールボックス

下から選んでください:

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