Target Language Compiler    

Scoping

The structure of the %with directive is

The %with directive adds a new scope to be searched onto the current list of scopes. This directive makes it easier to refer to block-scoped variables.

For example, if you have the following Real-Time Workshop file

You can access the Name parameter without a %with statement, by using

or by using %with

Variable Scoping

The Target Language Compiler uses dynamic scoping to resolve references to variables. This section illustrates how the Target Language Compiler determines the values of variables.

In the simplest case, to resolve a variable the Target Language Compiler searches the top-level Real-Time Workshop pool followed by the global pool. This illustration shows the search sequence that the Target Language Compiler uses.

Figure 5-4: Search Sequence

You can modify the search list and search sequence by using the %with directive.

Example

When you add the following construct

the System[sysidx] scope is added to the search list, and it is searched before anything else.

Figure 5-5: Modifying the Search Sequence

Using this technique makes it simpler to access embedded definitions. For example, to refer to the system name without using %with, you would have to use

Using the %with construct (as in the previous example), you can refer to the system name simply by

The rules within functions behave differently. A function has its own scope, and that scope gets added to the previously described list as depicted in this figure.

Figure 5-6: Scoping Rules Within Functions

For example, if you have the following code,

and if Name is not defined in foo, the assignment will use the value of name from the previous scope, CompiledModel.System[SysIdx].Name.


 Identifier Definition Target Language Functions