You are in: RPG Cafe > Why use the TEMPLATE keyword?
Short URL: https://ibm.biz/rpgcafe_template_keyword
RPG Cafe: The TEMPLATE keyword?
Why use the TEMPLATE keyword?
The TEMPLATE keyword was added to RPG in 6.1. When you use the TEMPLATE keyword on a standalone field or data structure, the compiler only allows you to use that definition for other LIKE or LIKEDS definitions, and for a couple of built-in functions, %SIZE and %ELEM.
Before 6.1, when people wanted to define a field or data structure to be used as a template, they often defined it as BASED on a pointer, where the pointer would never be set to a valid value. Others just added something like _t to the end of the name, as a visual signal that the variable was to be used as a template. And many people did both.
Here are some advantages to using the TEMPLATE keyword when you have a field or structure that you only want to use for other LIKE or LIKEDS definitions.
Using the TEMPLATE keyword makes it clear to other programmers that the field or data structure is not intended to be used as a program variable. Even when you add a suffix such as _t to the name, the keyword makes it crystal-clear to other programmers. And it also makes it clear to the compiler, so the compiler issues a diagnostic when some later programmer tries to use the variable in calculations.
Reduction of storage used by the program
A variable defined with the TEMPLATE keyword is not generated into the module, so no storage is associated with it at run time.
In the past, specifying the BASED keyword had a similar benefit regarding program storage at run time, because storage was only needed for the 16-byte pointer. When the same pointer was used for all such BASED keywords, say BASED(template), there was only one "wasted" pointer for the module.
Ability to define default initialization values for a data structure
You can code the INZ keyword for a template data structure or its subfields. Then, you can code INZ(*LIKEDS) for a LIKEDS data structure, to pick up those initializations.
Reduction of nonsense data produced in the debugger when you use %LOCALVARS to show all available fields
When you use EVAL %LOCALVARS in the system debugger, or "show all variables" in a visual debugger, it lists all the fields for that procedure. For a cycle-main procedure, it lists all the global variables. When you have several BASED(template) data structures in your module, the debugger lists those data structures with ***** as the value for each of the subfields. When you use the TEMPLATE keyword instead of the BASED keyword, the debugger does not know about those data structures.
Reduction of nonsense data produced in the formatted dump
Similar to the debugger with %LOCALVARS, the formatted dump lists all the data structures based on a null pointer, with the value of each subfield shown as "Not addressable". When the data structure is defined with the TEMPLATE keyword, the formatted dump does not know about the data structures.
16 December 2019