Why use the TEMPLATE keyword?
barbara_morris 120000DX5W Visits (12927)
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 will only allow you to use that definition for other LIKE or LIKEDS definitions, and for a couple of built-in functions, %SIZE and %ELEM.
Prior to 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 did both.
Here are some advantages to using the TEMPLATE keyword if 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 if 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 will issue a diagnostic if 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 runtime.
Using the BASED keyword had a similar benefit regarding program storage at runtime, because storage was only needed for the 16-byte pointer. If 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 listing all available fields
If you use EVAL %LOCALVARS in the system debugger, or "show all variables" in a visual debugger, it will list all the fields for that procedure. For a cycle-main procedure, it will list all the global variables. If you have several BASED(template) data structures in your module, the debugger will list those data structures with ***** as the value for each of the subfields. If you used the TEMPLATE keyword instead of the BASED keyword, the debugger would not know about those data structures.
Reduction of nonsense data produced in the formatted dump
Similar to the debugger with %LOCALVARS, the formatted dump will list all the data structures based on a null pointer, with the value of each subfield shown as "Not addressable". If the data structure is defined with the TEMPLATE keyword, the formatted dump will not know about the data structures.