## Tcl9.0.3/Tk9.0.3 Documentation > Tcl Commands > const

### Tcl/Tk Applications | Tcl Commands | Tk Commands | [incr Tcl] Package Commands | SQLite3 Package Commands | TDBC Package Commands | tdbc::mysql Package Commands | tdbc::odbc Package Commands | tdbc::postgres Package Commands | tdbc::sqlite3 Package Commands | Thread Package Commands | Tcl C API | Tk C API | [incr Tcl] Package C API | TDBC Package C API

### NAME

const — 定数を生成し初期化する Create and initialize a constant

### 構文 SYNOPSIS

const varName value

### 説明 DESCRIPTION

本命令は､その手続函数内部の定数を生成する為､若しくは､namespace eval 本体内部でその名前空間内部の常数を生成する為に､通常手続函数の本体内部で使われる｡const の成功裡の結果は常に空文字列である｡ This command is normally used within a procedure body (or method body, or lambda term) to create a constant within that procedure, or within a namespace eval body to create a constant within that namespace. The constant is an unmodifiable variable, called varName, that is initialized with value. The result of const is always the empty string on success.

もし､変数 varName が存在しない場合､値 value を設定する為に､値と共に生成され､そして､定数と印付けされる; これは､ それ以外の命令(譬えば､ set, append, incr, unset)は変数を修正や削除できないことを意味する; 変数は､どの様な追跡も呼ばれてない定数か否かを確認される｡もし変数 varName が既存であると､それが定数として印付けされてない限り不都合である｡ If a variable varName does not exist, it is created with its value set to value and marked as a constant; this means that no other command (e.g., set, append, incr, unset) may modify or remove the variable; variables are checked for whether they are constants before any traces are called. If a variable varName already exists, it is an error unless that variable is marked as a constant (in which case const is a no-op).

varName は､如何なる意味に於いても､条件付きの名前や配列の参照要素であってはならない｡もし変数が実在し､且つ配列の場合､不都合になる｡ The varName may not be a qualified name or reference an element of an array by any means. If the variable exists and is an array, that is an error.

定数は通常､それを含む手続き函数の終了や､名前空間の削除によって､削除される｡ Constants are normally only removed by their containing procedure exiting or their namespace being deleted.

### 用例 EXAMPLES

手続函数内に定数を生成する: Create a constant in a procedure:

    proc foo {a b} {
        const BAR 12345
        return [expr {$a + $b + $BAR}]
    }

正規表現の構成要素の一部を取除く為に､名前空間内に定数を生成する｡ Create a constant in a namespace to factor out a regular expression:

    namespace eval someNS {
        const FOO_MATCHER {(?i)}\mfoo\M}

        proc findFoos str {
            variable FOO_MATCHER
            regexp -all $FOO_MATCHER $str
        }

        proc findFooIndices str {
            variable FOO_MATCHER
            regexp -all -indices $FOO_MATCHER $str
        }
    }

繰返しの中に､定数作成は､不都合ではない: Making a constant in a loop doesn't error:

    proc foo {n} {
        set result {}
        for {set i 0} {$i < $n} {incr i} {
            const X 123
    	lappend result [expr {$X + $i**2}]
        }
    }

### SEE ALSO

proc, namespace, set, unset

### KEYWORDS

namespace, procedure, variable, constant
Copyright © 2023 Donal K. Fellows