class Class( _Parameter [= Default], ... ) class Class( = Type )
A Generic Class has parameters that are substituted into the source code in the Class source code and any generic procedures that belong to it. A list of Generic parameters follows the class name. Each parameter is a macro name with a leading underscore with an optional default value. When a Generic name appears within the Class file or any of its Generic member procedures, the name is replaced with the corresponding value.
To import a Generic Class add Generic arguments used to import the class and set the generic parameters. Then when source code contains a parameter name it will be substituted with the argument value for the parameter.
import Class( [Value], ... )
Unique Class, Type, and Procedure names are created when declaring a Generic Class. When a class is imported a compound name is created that can be used to reference an instance of a generic class. The compound name consists of the class name, two dots, and the first value. When Array(string) is imported the generic class is referenced as "Array..string". The first value is either a valid compound symbolic name or a decimal integer. Note that Gilda names are not case sensitive.
<<< In an application Class file my.stuff.clg >>> import Stack(string) :Import a stack of strings from any Class. <<< In file stack.clg >>> class Stack( _type ) is ... :Define a Generic stack structure (details elided). generic Push :The Push procedure is a member of the stack Class. : The Class name and structure type is: Stack..string : The procedure name is: Push..string <<< In file push.gg >>> method PUSH :Generic push procedure change Stack Stack..string :The first parameter must be the stack. entry Text string :Push this text on the stack. <<< In the application file my.stuff.g >>> method MY.STUFF local My.Stack Stack..string, Text string PUSH My.Stack, Text
Generic arguments and default values may be a symbol or a simple decimal integer without any underscores, leading zeros, or sign. When a Generic class is Imported without a parameter then the default specified on the Class declaration is used. Often symbolic macro values represent type names, but that is not a requirement.
In the second form of a Generic Class declaration, no parameter is designated. This creates a single class with the compound name "Class..value". No macro substitution occurs within the Class or its methods. The purpose of this is to create unique names for a Class and any generic procedures within it. This is particularly useful to avoid name conflicts. Generic proceduress can be called using a shorthand notation when the type of the first argument extends the procedure name in the call.
<<< In the Generic Class file text.string.clg >>> class Text.String(=string) :The compound class name is: Text.String..string generic Proper :The compound method name is: Proper..string <<< In the application file my.stuff.g >>> Name = Proper( Item ) :When Item is a string call: Proper..string
Withing a program the As clause on an Import declaration can be helpful. It denotes an alias name for the imported class. The alias can be simpler than the full class name and more meaningful within the context of the program.
import Stack(string, 100) as Small.Stack local Stack Small.Stack
The Import As clause can also disambiguates generic classes with multiple parameters. Only the first parameter is user as the full class name. In the following example both full class names would be "Stack..string" and would collide. Using the aliases resolves the name conflict.
import Stack(string, 100) as Small.Stack import Stack(string, 1000) as Big.Stack