QuantumC

User Types

Back to Home

Types of User Types:

//// Use Syntax Pro/Con
Struct Storing a group of variables struct name { field-type field-name; …}; Very useful and simple
Union Defines a set of types or literals that a variable of this type can be type union-name = type-orliteral1 | type-orliteral2…..; Amazingly powerful for much more than just making a variable of multiple types
Alias Aliases a type to a name type alias-name = type-to-alias; Simple, just aliasing a name
Enum Stores a collection of name -> literal mappings enum name = { fieldone = value; …..}; Can do something similar with unions, however enums are more usefull and explicit for this use.

Syntax And examples

Struct syntax is:

struct struct-name {
    field-type field-name;
    ...
}

For Example

struct Point {
    int x;
    int y;
    int z;
}

Union syntax is very simple. It is as follows

type union-name = type1 | type2.....

For Example

type Number = int | double | float | "1";

Alias syntax is

type name = thing;

So

type IntAlias = int;

And Enum syntax is

enum name {
    name1 = value1;
    name2 = value2;... //If you don't set the values it will auto increment from 0 -> ...
};

So an example would be

enum Status {
    ERROR = 1;
    TIMEOUT = 2;
    OK = 0;
};