スカラー変数を暗黙的に宣言する

モジュールレベルまたはプロシージャ内で、まだ宣言していない識別子に値を代入することにより、変数を暗黙的に宣言できます。例えば、次のようになります。

' Create an Integer variable without declaring it explicitly 
' and initialize it to 1.
counter% = 1

これは、次の明示的宣言とステートメントと同じ効果になります。

Dim counter%
counter% = 1

明示的に宣言される変数と同じく、識別子は LotusScript で有効なものでなければならず、同一モジュール内の同一スコープで定数、変数、またはプロシージャの名前として使用されていてはいけません。宣言時に変数名にデータ型の接尾辞を付けた場合、その接尾辞が変数のデータ型を決定します。データ型の接尾辞を付けない場合、次のいずれかになります。まず、名前が既存の Deftype ステートメントで扱われる文字で始まる場合は、変数はそのステートメントに適切なデータ型に暗黙的に宣言されます。それ以外の場合は、変数は Variant 型になるように暗黙的に宣言されます。宣言に As dataType 節が含まれず、変数名の後ろにデータ型の接尾辞が付いていない場合、明示的に宣言された変数に対して同じ規則が適用されます。

' Declare a variable of type Variant.
Dim myVarV

暗黙的な宣言は、変数を明示的に宣言するために必要となるコードの行数を節約できるので、簡単なスクリプトを作成する場合には便利な早道です。しかし、変数宣言とステートメントへの値の代入をまとめてコード行を節約すると、次の 2 つの理由からアプリケーションが複雑になる可能性があります。

以下に例を示します。

' Create the Integer variable anInt without explicitly 
' declaring it and initialize it to 10.
anInt% = 10
Print anInt
' Produce "Name previously declared" error
' because LotusScript reads anInt (without suffix character)
' as an implicitly declared Variant variable, not
' the Integer variable anInt% (with suffix character).

' Create the Variant variable myVariantV without explicitly
' declaring it and initialize it to 10.
myVariantV = 10   
Print myVariantV%
' Produce "Type suffix mismatch" error
' because myVariantV (without suffix character) was declared
' as type Variant, but the suffix character % is only 
' appropriate for variables declared as type Integer.

LotusScript® アプリケーションで暗黙的な宣言を禁止する場合、アプリケーションの実行時にロードする予定のモジュールに、モジュールレベルで Option Declare ステートメントを含めます。このステートメントは、LotusScript に対して、すべての変数を明示的に宣言することを要求します。

注: Boolean データ型と Byte データ型には、データ型接尾辞がないので、暗黙的に宣言することはできません。

Deftype ステートメント

Deftype ステートメントは、名前が特定の英文字で始まり、後ろにデータ型接尾辞が付いておらず、As dataType 節を含む明示的宣言にはない変数に対して、デフォルトのデータ型をモジュールレベルで割り当てるために使用します。ステートメントは、モジュール内でどの変数宣言よりも前に指定しなければなりません。構文は次のとおりです。

Def type range [, range]...

ここで type は Cur や Dbl などの接尾辞で、データ型の省略形です。range は 1 つ以上の連続した英文字です。

以下に例を示します。

' Implicitly declared variables beginning with
' A, a, B, b, C, or c will be of type Integer.
DefInt A-C
' Create the Integer variable anInt on the fly 
' and initialize it to 10.
anInt = 10
' Create a variable of type Variant on the fly
' and initialize it to 2. It's a Variant because
' it doesn't have a data type suffix character and doesn't
' begin with any of the characters in the specified
' DefInt range. 
smallIntV = 2