Curl® 言語のソースコードは、テキストと評価される式のコンビネーションで構成されます。この章では、Curl 言語の式の基本形について説明します。具体的には、以下の項目について説明します。
要約: | - オブジェクトに属するクラス メンバにアクセスするために使用します。
- 構文: object-name.member-name
|
フィールド アクセス式はオブジェクトに属するクラス メンバを参照するために使用します。オブジェクト、クラス、クラス メンバの詳細は、「
クラス」の章を参照してください。フィールド アクセス式を使用して、フィールド、オプション、ゲッター、セッターを参照できます。フィールド アクセス式の形式を次に示します。
object-name.member-name
ここで object-name はオブジェクト名で、 member-name はメンバ名です。object-name にはオブジェクトを返す式も指定できますが、member-name は単純な識別子である必要があります。たとえば、my-stringbuf.clear はフィールド アクセス式で、ここで my-stringbuf は StringBuf 型のオブジェクトで、clear は StringBuf のメンバです。
フィールド アクセス式を使用して、必要に応じてクラス メンバの値を取得または設定します。次にアクセッサとメソッドを起動するフィールド アクセス式の例を示します。
例:
フィールド アクセス式によりゲッターを呼び出す |
|
{value
|| Declare a variable "str" to be an instance of the String
|| class and initialize it with the value "Hello World!"
let str:String = {String "Hello World!"}
|| Calls the size getter for the object str,
|| where "size" is a getter for String objects that
|| returns the number of characters in a string.
str.size
}
| |
例:
フィールド アクセス式によりメソッドを起動 |
|
{value
|| Declare a variable "str" to be an instance of the StringBuf
|| class and initialize it with the value "Hello world".
let str:StringBuf = {StringBuf "Hello world"}
|| Call the "concat" method for the object str,
|| where "concat" is a method of StringBuf that concatenates
|| its argument to the end of the current object.
{str.concat ", here comes Curl!"}
|| Return the new value of str,
str
}
| |
要約: | - コレクション内の各要素にアクセスするために使用します。
- 構文: collection[index]
|
配列アクセス式を使用してオブジェクトのコレクションの要素を参照します。また、配列アクセス式を使用して、配列、ハッシュ テーブル、文字列の要素を参照します。配列アクセス式の形式を次に示します。
collection[index]
ここで
collection は値が配列、ハッシュ テーブル、または
String の式で、
index は参照する要素のインデックスまたはキー (ハッシュテーブルの場合) として評価します。次に例を示します。
- my-array[0] は my-array の要素 0 を参照します。
- my-array[3] は my-array の要素 3 を参照します。
- my-string[1] は my-string の char 型 1 を参照します。
- my-hash-table["cat"] は my-hash-table のキー "cat" に対応する要素を返します。
配列アクセス式を使用して、必要に応じて要素の値を取得または設定します。
配列と文字列の要素を参照する際は、index は整数値として評価することが必要です。つまり、index はそれ自体 int 型である必要はありませんが、暗黙的または明示的に int 型にキャストされることが必要です。次に例を示します。
- my-array[1.2] は暗黙的にキャストされて、my-array の要素 1 を参照します。
配列と文字列のそれぞれにおいて、配列の最初の要素または文字列の左端の文字を
0 として順に番号が付けられます。配列の詳細は「
配列」のセクション、文字列の詳細は「
文字列」のセクションを参照してください。配列と文字列の要素にアクセスする例を次に示します。
例:
配列の要素へのアクセス |
|
|| Declare a variable "arr" to be an array of integers
|| and initialize it.
{let arr:{Array-of int} = {{Array-of int} 98, 99, 100, 101, 102, 103}}
|| Display element "3".
{value arr[3]}
|| Set element with index "3" to have the value "13".
|| Because element "0" is the first element of an array,
|| element "3" is the fourth element.
{set arr[3] = 13}
|| Display element "3".
{value arr[3]}
| |
例:
文字列の文字へのアクセス |
|
{value
|| Declare a variable "str" to be a String and initialize
|| the string with the value "Manchester".
let str:String = "Manchester"
|| Return element "2" of the string.
|| Because the element "0" is the first character in a string,
|| element "2" is the third character.
str[2]
}
| |
ハッシュテーブルでは、
indexはハッシュテーブル内の有効なキーと一致することが必要です。指定するキー値のタイプはハッシュテーブルによって異なります。ハッシュテーブルの詳細は「
ハッシュテーブル」のセクションを参照してください。
例:
ハッシュテーブル要素へのアクセス |
|
{value
|| Declare a variable "hash" to be a hash table and
|| initialize it.
let hash:HashTable =
{HashTable
"first", "hats",
"second", "purses",
"third", "gloves"
}
|| Return the element that corresponds to key "second".
hash["second"]
}
| |
要約: | - 変数名、フィールド アクセス式、配列アクセス式に値を代入するために使用します。
- 構文:[{]set expression-list = expression2[}]
- 値の増減には inc と dec を使用します。
|
代入ステートメントを使用して、変数名、フィールド アクセス式、配列アクセス式に値を代入します。代入ステートメントの構文を次に示します。
中カッコの囲みはトップ レベルのコードでは必須ですが、コード ブロック内ではオプションです。
次の表に有効な代入ステートメントの例を示します。
式のタイプ | 例 |
変数 | set var = 30 |
フィールド | set obj.field = true |
オプション | set obj.option = true |
配列要素 | set a[1] = "gloves" |
StringBuf 要素 | set sb[5] = 'e' |
ハッシュテーブル要素 | set h["cat"] = 37 |
複数の値 | set (x, y) = {return-2-values} |
代入ステートメントの代わりに、
inc と
dec を値の増減に使用できます。
inc は指定した量の値を増加させます。同様に、
dec は指定した量の値を減少させます。
これらの式の構文を次に示します。
構文: | {inc var [, delta = step] } または {dec var [, delta = step] } |
|
var | 値の増減を行う変数名、フィールド アクセス式、配列アクセス式です。 |
step | var に追加する (incの場合)、または差し引く (dec の場合) オプションの数値です。delta = step が指定されていない場合は、既定の delta = 1 が適用されます。 |
|
Copyright © 1998-2019 SCSK Corporation.
All rights reserved.
Curl, the Curl logo, Surge, and the Surge logo are trademarks of SCSK Corporation.
that are registered in the United States. Surge
Lab, the Surge Lab logo, and the Surge Lab Visual Layout Editor (VLE)
logo are trademarks of SCSK Corporation.