Menu

Generics

Will Pittenger

Overview

Generics in ASIL are closer to .NET generics most closely in usage than Java. That's because Java generics are more like "pseudo-generics". Java strips the type information out and converts the generics into their raw form. This makes it impossible to access information about the type parameters at run time. The syntax is loosely adapted from C#.

Note: The names of your type parameters must be unique inside whatever is generic. So if you have a generic class with a type parameter named "x", "x" can't be the name of any member.

What can and can't be generic

The table below shows what can and can't be generic. Note while the various types of variables and properties themselves can't be generic, they can be instances of generics.

Identifier type Yes or no
Bitfield
Class
Command
Complex Statement
Conditional variable
Constant
Constructors
Creators
Data member variable
Destructors
Function
Global variable (DASIL)
Interface
Local variable
Operator
Primitive-based enum
Properties
Structure
Structure-based enum
Type alias ✓ (if based on a generic)
Union with external controlling member
Union with internal controlling member

Types of generic type parameters

ASIL generic type parameters fall into several categories. Those are primitives, enums, structs, struct-based enums, and classes. [Delegates] and [Events] are considered to be instances of classes.

A simple generic class

The class below takes two type parameters. Notice the type parameters go between the class keyword and the class's identifier. This will be explained with the talk on generic procedures. These samples don't show interfaces, structures, unions, or type aliases; but the syntax is the same.

class <a, b> Name
  public function GoLeft a HowMuch
      returns b
    ' Do something with HowMuch and return a variable of type b

A generic class with restrictions on the type parameters

If you need to ensure the passed type has some members, the where clause comes in handy. This class requires a passed type to derive from X and implement IComparable.

class <a> Name
    where
      a
        inherits X
        implements IComparable
  ' Make use of A

This generic class takes type parameters with a different type of restriction.

class <a, b, c, d> Class2
    where
      a = enum ' must be an enum
      b = struct enum ' must be a struct-based enum
      c = struct ' must be a struct or class
      d = class ' Must be a class

Generic procedures

Commands, functions, and complex structures can all be generic. The code sample below shows samples of each. The returns and where clauses for generic functions can occur in any order. (Note: The samples only show prototypes.)

command <a> GenericCommand var a A
  where
    a = enum

function <c> GenericFunction var c C
  returns c
  where
    c
      implements IComparable

statement <g> GenericStatement var ref g G
  where
    g = struct

Now you should be able to see why ASIL puts the declaration of type parameters before the name of what is being declared. If the order were command GenericCommand<a> var a A", would "<a>" be part of the syntax or the type parameters?

Referencing a generic class

Expanding a generic class is similar to C++, C#, and Java. Use the syntax shown below.

var GenericClass<String> name = new GenericClass<String>

Calling a generic procedure

Now that ASIL is expecting type parameters, it can expect, and even require, them. So use this syntax.

GenericCommand<EnumType> myEnumInstance

var String name 
name = GenericFunction<String> "test"

GenericStatement<MyStruct> var MyStruct myStructInstance
  ' Do something

Covariance and Contravariance

It is unknown how useful Contravariance is. But Covariance can be used. Both are shown here, but Contravariance might not survive.

var GenericClass<extends String> ' Covariance
var GenericClass<base String> ' Contravariance

Related

Wiki: Delegates
Wiki: Events
Wiki: Home
Wiki: keywords-class
Wiki: keywords-where

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.