Templates
- Templates are a C++-style mechanism for instantiating specialized classes; they are not like Java-style generics.
- Templates are to classes what classes are to objects. You can define a single template and generate several classes from it as necessary.
- You write a template class like you would a normal class except that certain key types are defined as placeholder types rather than being a specific type.
- When you refer to the template in code you must supply exact types to substitute for the placeholder types. Each unique template reference causes the compiler to generate an entirely new class that includes the unique substitution types in lieu of the placeholder types. At run-time, instanced template classes are just as fast and efficient as non-template-based classes (because at run-time they are just another class).
- Template placeholder types can have default substitution types in the same way that methods can have default parameter values. Default substitution types may specify earlier placeholder types, in which case both placeholder types are assigned the same substitution type.
- Compounds can be defined as templates.
Example:
class Test
METHODS
method init:
local Pair<<String>> name( "Abe", "Pralle" )
local Pair<<Int32,Radians>> half_circle( 180, pi )
println( "name: $" (name) )
println( "half_circle: $" (half_circle) )
endClass
class Pair<<FirstType,SecondType=FirstType>>
PROPERTIES
first : FirstType
second : SecondType
METHODS
method init( first, second );
method to_String.String: return "[$,$]" (first,second)
endClass
- Login to post comments
