| Target Language Compiler | ![]() |
The structure of the %with directive is
%with expression %endwith
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
System {
Name"foo"
}
You can access the Name parameter without a %with statement, by using
%<System.Name>
%with System %<Name> %endwith
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.

You can modify the search list and search sequence by using the %with directive.
Example
When you add the following construct
%with CompiledModel.System[sysidx] ... %endwith
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
CompiledModel.System[sysidx].Name
Using the %with construct (as in the previous example), you can refer to the system name simply by
Name
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,
% with CompiledModel.System[sysidx] . . . %assign a=foo(x,y) . . . %endwith . . . %function foo (a,b) . . . assign myvar=Name . . . %endfunction
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 | ![]() |