OSDN Git Service

原文を8章まで追加. ミスで訳していなかった箇所の修正.
[omake-japanese/omake_trans.git] / language-naming.rst
1 .. 5-language-naming
2
3 .. index::
4    single: 修飾子
5    single: 名前空間
6 .. _label5:
7
8 5. 変数と名前空間
9 ==================================
10 .. During evaluation, there are three different kinds of namespaces. Variables can be private, or they may refer to fields in the current this object, or they can be part of the global namespace. The namespace can be specified directly by including an explicit qualifier before the variable name. The three namespaces are separate; a variable can be bound in one or more simultaneously.
11
12 コードを評価する際、OMakeの変数には3つの異なる種類の名前空間があります。変数は *プライベート* なものにしたり、 *現在の* オブジェクトのプロパティを参照したり、 *グローバル* な名前空間の一部として用いることができます。名前空間を指定するには、変数名の前に修飾子を直接明示する必要があります。この3つの名前空間は分割されており、変数は一つ、あるいはさらに多くの名前空間に束縛することができます。 ::
13
14     # プライベートな名前空間
15     private.X = 1
16     # 現在のオブジェクト
17     this.X = 2
18     # パブリック、グローバルに定義された名前空間
19     global.X = 3
20
21 .. index::
22    single: private.
23    single: export
24 .. _label5.1:
25
26 5.1 private.
27 ----------------------------------
28 .. The private. qualifier is used to define variables that are private to the current file/scope. The values are not accessible outside the scope. Private variables are statically (lexically) scoped.
29
30 ``private.`` 修飾子は変数が現在のファイルやスコープ上のプライベートなものであると定義したい場合に用います。値は外のスコープから参照することができません。プライベートな変数は静的にスコープされています。 ::
31
32     Obj. =
33        private.X = 1
34
35        print() =
36           println(The value of X is: $X)
37
38     # 出力:
39     #    The private value of X is: 1
40     Obj.print()
41
42     # XはObj内のプライベート変数なので、エラーとなります
43     y = $(Obj.X)
44
45 .. In addition, private definitions do not affect the global value of a variable.
46
47 加えて、プライベート変数はグローバルな変数の値に影響を及ぼしません。 ::
48
49    # パブリックなxの値は1
50    x = 1
51
52    # このオブジェクトはxのプライベートな値を使用しています
53    Obj. =
54        private.x = 2
55
56        print() =
57           x = 3
58           println(The private value of x is: $x)
59           println(The public value of x is: $(public.x))
60           f()
61
62    # 出力:
63    #    The private value of x is: 3
64    #    The public value of x is: 1
65    Obj.print()
66
67 .. Private variables have two additional properties.
68
69      1. Private variables are local to the file in which they are defined.
70      2. Private variables are not exported by the export directive, unless they are mentioned explicitly.
71
72 プライベート変数は2つの性質を持っています。
73
74 #. プライベート変数は定義されたファイルしか参照できません。
75 #. プライベート変数はたとえ明示的に ``export`` 文を使用したとしてもエクスポートできません。 ::
76
77          private. =
78             FLAG = true
79
80          section
81             FLAG = false
82             export
83
84          # FLAGはまだtrueです
85          section
86             FLAG = false
87             export FLAG
88
89          # FLAGは現在falseです
90
91 .. index::
92    single: this.
93 .. _label5.2:
94
95 5.2 this.
96 ----------------------------------
97 .. The this. qualifier is used to define fields that are local to an object. Object variables are dynamically scoped.
98
99 ``this.`` 修飾子はオブジェクトのローカルなプロパティについて定義したい場合に用います。オブジェクト変数は動的にスコープされます。 ::
100
101     X = 1
102     f() =
103        println(The public value of X is: $(X))
104
105     # 出力:
106     #    The public value of X is: 2
107     section
108        X = 2
109        f()
110
111     # Xはオブジェクト中の保護されたプロパティを表しています。
112     Obj. =
113        this.X = 3
114
115        print() =
116           println(The value of this.X is: $(X))
117           f()
118
119     # 出力:
120     #    The value of this.X is: 3
121     #    The public value of X is: 1
122     Obj.print()
123
124     # この文は正しく、Yには3が束縛されます。
125     Y = $(Obj.X)
126
127 .. In general, it is a good idea to define object variables as protected. The resulting code is more modular because variables in your object will not produce unexpected clashes with variables defined in other parts of the project.
128
129 一般的に、保護されたオブジェクト変数を定義することは良いとされています。プロジェクトの他の部分で定義された変数と被ってしまったために起こる、意図しないバグを生み出さないためです。その結果、コードはよりモジュール化されます。
130
131 .. index::
132    single: global.
133 .. _label5.3:
134
135 5.3 global.
136 ----------------------------------
137 .. The global. qualifier is used to specify global dynamically-scoped variables. In the following example, the global. definition specifies that the binding X = 4 is to be dynamically scoped. Global variables are not defined as fields of an object.
138
139 ``global.`` 修飾子はグローバルに動的なスコープを持つ変数を定義したい場合に用います。以下の例では、 ``global.`` 宣言は束縛文 ``X = 4`` が動的にスコープされた変数であることを指定しています。グローバル変数はオブジェクトのプロパティとして定義することが *できません* 。 ::
140
141     X = 1
142     f() =
143        println(The global value of X is: $(X))
144
145     # 出力:
146     #    The global value of X is: 2
147     section
148        X = 2
149        f()
150
151     Obj. =
152        protected.X = 3
153
154        print() =
155           println(The protected value of X is: $(X))
156           global.X = 4
157           f()
158
159     # 出力:
160     #    The protected value of X is: 3
161     #    The global value of X is: 4
162     Obj.print()
163
164 .. index::
165    single: protected.
166 .. _label5.4:
167
168 5.4 protected.
169 ----------------------------------
170 .. In OMake 0.9.8, protected is a synonym for this.
171
172 OMake 0.9.8では、 ``protected`` は ``this`` 修飾子と同義語でした。 ::
173
174     osh>protected.x = 1
175     - : "1" : Sequence
176     osh>value $(this.x)
177     - : "1" : Sequence
178
179 .. In 0.9.9, this will change, so that the qualifier protected means (in 0.9.9) that a variable is local to the current object or file, and may not be accessed outside it.
180
181 0.9.9ではこの仕様は変更され、 ``protected`` 修飾子は変数が現在のオブジェクトまたはファイルについてのローカル変数とし、外部からアクセスできないようにしたい場合に用います。
182
183 .. index::
184    single: public.
185 .. _label5.5:
186
187 5.5 public.
188 ----------------------------------
189 .. In OMake 0.9.8, public is a synonym for global.
190
191 OMake 0.9.8では、 ``public`` は ``global`` 修飾子と同義語でした。 ::
192
193     osh>public.x = 1
194     - : "1" : Sequence
195     osh>value $(global.x)
196     - : "1" : Sequence
197
198 .. In 0.9.9, this will change, so that the qualifier public means (in 0.9.9) that a variable is to be accessible from outside the current file or object.
199
200 0.9.9ではこの仕様は変更され、 ``public`` 修飾子は変数が現在のファイルまたはオブジェクトの外部からアクセスできるようにしたい場合に用います。
201
202 .. _label5.6:
203
204 5.6 修飾されたブロック
205 ----------------------------------
206 .. If several qualified variables are defined simultaneously, a block form of qualifier can be defined. The syntax is similar to an object definition, where the name of the object is the qualifier itself. For example, the following program defines two private variables X and Y.
207
208 いくつかの修飾された変数が同時に定義された場合、修飾子のブロックが優先的に定義されます。文法はオブジェクトの定義と似ていますが、それはオブジェクト名それ自体が修飾子だからです。例えば、以下のプログラムはプライベート変数 ``X`` と ``Y`` を定義しています。 ::
209
210     private. =
211         X = 1
212         Y = 2
213
214 .. The qualifier specifies a default namespace for new definitions in the block. The contents of the block is otherwise completely general.
215
216 修飾子はブロック内で新しく定義された変数の、デフォルトの名前空間を指定しています。その違いを除いて、ブロックの内容は完全に通常のコードとしてふるまいます。 ::
217
218     private. =
219         X = 1
220         Y = 2
221         public.Z = $(add $X, $Y)
222         # "The value of Z is 3" と出力される
223         echo The value of Z is $Z
224
225 .. index::
226    single: declare
227    single: 明示的に修飾されていない変数
228 .. _label5.7:
229
230 5.7 変数宣言
231 ----------------------------------
232 .. When a variable name is unqualified, its namespace is determined by the most recent definition or declaration that is in scope for that variable. We have already seen this in the examples, where a variable definition is qualified, but the subsequent uses are not qualified explicitly. In the following example, the first occurrence of $X refers to the private definition, because that is the most recent. The public definition of X is still 0, but the variable must be qualified explicitly.
233
234 変数名が修飾されていない場合、その名前空間は最も近くで定義された名前空間か、この変数が定義されているスコープの名前空間が用いられます。私たちは以前すでにこの現象を例を通して見ています。その例では、変数の定義が修飾されていても、その後に来る変数は明示的に修飾されていなかったはずです。以下の例では、最初に宣言された ``$X`` は *プライベート* な変数 ``$(private.X)`` が関連付けられています。なぜならこれは最も近くで定義されているからです。パブリックな変数 ``X`` は未だに ``0`` であり、この変数を指定するためには明示的に修飾しなければなりません。 ::
235
236     public.X = 0
237     private.X = 1
238     
239     public.print() =
240         println(The value of private.X is: $X)
241         println(The value of public.X is: $(public.X))
242
243 .. Sometimes it can be useful to declare a variable without defining it. For example, we might have a function that uses a variable X that is to be defined later in the program. The declare directive can be used for this.
244
245 時々、修飾子を定義する事なしに変数宣言することが有効である場合があります。例えば、私たちはプログラムの後ろで定義された変数 ``X`` を用いる関数を持っていたとしましょう。 ``declare`` 文はこのような場合に使うことができます。 ::
246
247     declare public.X
248
249     public.print() =
250         println(The value of X is $X)
251
252     # "The value of X is 2" と出力される
253     X = 2
254     print()
255
256 .. Finally, what about variables that are used but not explicitly qualified? In this case, the following rules are used.
257
258       * If the variable is a function parameter, it is private.
259       * If the variable is defined in an object, it is qualified with this..
260       * Otherwise, the variable is public. 
261
262 最後に、明示的に修飾されていない変数についてはどうなるのでしょうか?このような場合、以下のルールが使われます。
263
264 * もし変数が関数の引数ならば、その変数はプライベートとなります。
265 * もし変数がオブジェクト中で定義されているならば、 ``this.`` 修飾子で定義されます。
266 * それ以外はすべてパブリックとなります。