5.5.2:Local Variables

ローカル変数

A local variable can be used only in the handler that creates it. Once the handler finishes executing, the variable is deleted. The next time you execute the handler, the variable starts from scratch: it does not retain what you put into it the last time the handler was executed.

ローカル変数は、その変数を作ったハンドラ内でのみ有効です。ハンドラの実行が終了すると、ローカル変数は消滅してしまいます。次の同じハンドラを実行すると、変数はまた最初から作られます:つまり、ハンドラは前回実行されたときに変数に入れた値を保持し続けられないのです。

To create a local variable, you simply put something into it. If you use the
put command with a variable name that does not yet exist, the variable is automatically created as a local variable:

ローカル変数を作りには、単純に変数に何かを入れるだけです。putコマンドをまだ使われていない変数名とともに使うと、変数は自動的にローカル変数となります。


  put true into myNewVar -- creates variable named "myNewVar" (myNewVarという名の変数を作成)


Tip: While you can use almost any word that isn’t a Transcript language word – also known as a reserved word – for a variable name, it will help you greatly if you get into the habit of naming variables logically and consistently. For details of what names are allowed for variables, see the section on Variable Names below. For some recommendations on naming variables, see the section on Good Design Recommendations.

秘訣: トランスクリプト言語で使われていない ー つまり、予約語ではない ー 限り、変数名にどんな単語を使ってもかまいません。変数名を論理的且つ終始一貫して扱う癖をつけることは、非常に役立つことです。変数名をどうつけるかについての詳細は、以下のVariable Namesセクションをご覧下さい。変数の上手な名付け方については、Good Design recommendationsセクションを参考にしてください。

Alternatively, you can create a local variable explicitly by declaring it using the
local command inside a handler:

ローカル変数を作る今一つの方法は、ハンドラ内でlocalコマンドを使って明示的に宣言することです。

Important: If you use a local variable in one handler, and that handler calls another handler, you can't use the local variable in the second handler. If you use a variable with the same name, Revolution creates a second variable that is local to the second handler.

重要: あるハンドラ内でローカル変数を使う場合、更にそのハンドラが他のハンドラを呼び出しているような場合、二番めのハンドラ内でそのローカル変数を使うことはできなくなります。ある変数を同じ名前で使うと、レヴォルーションは二番めのハンドラ用に二番めのローカル変数を作ってしまいます。

But the two local variables don't affect each other, because they're in different handlers.
In the following example, the two handlers each have a local variable named "myVar", but they are different local variables because they're in different handlers, and changing one does not affect the other:

しかし、二つのローカル変数は異なるハンドラにあるため互いに独立していて干渉しあいません。
次の例では、二つのハンドラはそれぞれ「myVar」という名のローカル変数を持っています。しかし、この二つは異なるハンドラにあるため独立したローカル変数であって、一つの変数が変更されても、もう一つの変数にはなんら影響しません:

 on mouseUp
  put 1 into myVar -- creates a local variable(ローカル変数を作ります)
  doCalledHandler
  answer myVar -- displays "1", not "2"(2ではなく1を表示します)
 end mouseUp

 on doCalledHandler
  put 2 into myVar -- creates a different variable with the same name(同じ名前のもう一つの変数を作ります)
 end doCalledHandler

 local myNewVar -- creates variable named "myNewVar"(変数名「myNewVar」を作ります)
 put true into myNewVar -- puts a value into "myNewVar"(「myNewVar」に値を入れます)


One common source of bugs involves misspelling a local variable name. Normally, doing so doesn't produce an execution error, because if you use a variable that doesn't exist, Revolution creates it automatically. This means that if you misspell a variable name, Revolution creates a new variable with the mispelled name. Such a bug may be difficult to track down because it can result in a variable having the wrong value without causing an error message.

バグに見られる共通の原因の一つにローカル変数名の書き間違いがあります。通常は書き間違いをしても実行エラーにはなりません。と言うのも、存在しない(宣言していない)変数を使ったとしても、レヴォルーションが自動的にその変数を作ってしまうからです。つまり、変数名を書き間違えても、レヴォルーションはその書き間違えた名前で変数を自動的に作ってしまいます。(そもそも、レヴォルーションには名前を書き間違えたのか正しい名前なのかを判断する能力はないわけです。)こうしたバグは、エラー・メッセージとして明示されないまま誤った値を変数の結果として保持してしまう為、追跡して発見するのが非常に困難なものです。

To prevent this problem, you can require all local variables to be declared with the
local command. You do this by turning on Script -> Variable Checking in the Script Editor menu bar. If this option is on, trying to use a local variable that doesn't exist will cause an execution error, instead of automatically creating it. Any misspelled variable names will therefore cause an obvious execution error when their handler is executed, making them easy to find.

こうした問題を引き起こさない為には、localコマンドを使って全ての変数を宣言することが必要となります。スクリプト・メニュー・バーのScript→Variable checkingをオンにすることで宣言をチェックできます。このオプションがオンになっていると、存在しないローカル変数を使おうとすると、レヴォルーションが自動的にその変数を作るのではなく、実行エラーになります。これによって、ハンドラが実行されたときに、書き間違えた変数名全てが明らかな実行エラーとして明示され、バグの発見が簡単になります。

Local variables are deleted when the handler that they're used in finishes executing. You can also use the
delete variable command to delete a local variable.

ローカル変数は、それを使用するハンドラの実行が終了すると消滅してしまいます。また、delete variableコマンドを使ってローカル変数を消去することもできます。