class Class( _Parameter [= Default], ... )
class Class( = Type )
A Generic Class has parameters that are substituted into the Class`s source code and any generic procedures that belong to it. A list of Generic parameters follows the class name. Each parameter is the name of a macro with a leading underscore and an optional default value. When a Generic parameter name appears within the Class file or any of its Generic members, the name is replaced with the corresponding value.
To import a Generic Class list Generic arguments to substitute into the Class for each Generic parameter. Then wherever source code contains a parameter name it will be substituted with the argument value for that parameter.
import Class( [Value], ... )
Unique Class, Type, and Procedure names are created by declaring a Generic Class. When a Class is imported a compound name for the Class is created that can be used to uniquely reference an instance of a Generic Class. The compound name consists of the Class name, two dots, and the value of the first parameter. When Array(string) is imported the Generic Class is referenced as "Array..string". The first value can either be a valid compound symbolic name or a decimal integer. Note that names in Gilda 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,
Item string
PUSH My.Stack, Item; Push the Item string on the stack.
Generic arguments and default values may be a symbol or a simple decimal integer without any underscores, leading zeros, or a sign. When a Generic Class is Imported without a parameter then the default specified on the Class declaration is used. Usually symbolic macro values represent type names, but that is not a requirement.
The second form of a Generic Class does not designate any parameter name. 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 procedures can be called using a shorthand notation so that 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 used 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