c:variable

<c:variable
  name = identifier
  value? = expression
  as? = type_name
  visibility? = "public" | "private" | "final" | "abstract" | "hidden" >
  <!-- Content: sequence-constructor -->
</c:variable>

Defines a variable.

Category
declaration
instruction
Permitted parent elements
c:module
c:override
c:package
Any XCST element whose content model is sequence-constructor
Any literal result element

Attributes

as The type of the variable.
name The name of the variable.
value The initial value of the variable.
visibility Specifies how the current component can be used in other (using) packages.

In addition to the attributes in the preceding table, there are a number of standard attributes that may appear on any XCST element.

Scope of Variables

A c:variable or c:param whose parent is c:module, c:package or c:override is a global variable. A c:variable or c:param that is not global is a local variable.

Global variables are visible to all other components in the containing package.

Initialization of Variables

The value of a variable can be supplied by the value attribute or by its contents (child nodes).

Global variables (except for required package parameters) are initialized lazily when the variable is referenced and evaluated for the first time. If not referenced, or if assigned before evaluating for the first time, then its initialization expression or instructions are never executed. The order of global variables is not significant.

Local variables are initialized when the evaluation of the containing sequence constructor reaches the variable declaration.

Both global and local variables can be left uninitialized by ommiting both the value attribute and contents. A value can then be assigned using the c:set instruction, or from C# code.

Values and Types of Variables

The type of a variable can be specified by the as attribute, or inferred if possible. For global variables, type inference does not work when the value is supplied by the value attribute.

The table below summarizes.

value attribute as attribute content effect
present absent empty Value is obtained by evaluating the value attribute. For local variables, the type is inferred from the expression. For global variables, the type is Object.
present present empty Value is obtained by evaluating the value attribute, casted to the type required by the as attribute.
present absent present Compilation error
present present present Compilation error
absent absent empty Local variables are not initialized. Global variables are initialized with null. The type is Object.
absent present empty Local variables are not initialized. Global variables are initialized with default(T), where T is the type required by the as attribute.
absent absent present Value is obtained by evaluating the sequence constructor. The type is inferred from the content (see next section).
absent present present Value is obtained by evaluating the sequence constructor, using the required sequence type specified by the as attribute.

Type Inference from Content

When the as attribute is omitted, XCST tries to infer the type of the variable from the content. If not successful, the fallback type is Object[].

content type
Text node String
Literal result element XElement
c:array Object
c:delegate Xcst.XcstDelegate<TItem>
c:document XDocument
c:element XElement
c:map Object
c:object Inferred by the expression
c:serialize String
c:text String
c:value-of String

When using multiple elements of the same type, the inferred type is an array of that type.

Note: Differences with xsl:variable

c:variable does not support qualified names. c:variable is mutable and can be left uninitialized. No implicit zero-length string or document node. Text nodes are compiled to string, which make string a first-class citizen in XCST. Parentless text nodes don’t exist in XCST.

Error Conditions

It is a compilation error if the value attribute is present when the content of the element is non-empty.

It is a compilation error if the visibility attribute is used on a local variable.

It is a compilation error if a global variable with visibility='abstract' has a default value.