aboutsummaryrefslogtreecommitdiff
path: root/docs/notes/generics_syntax.txt
blob: d78c5c1ecab38bb9bd58835f0263d7b6593c70de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49


syntax for arrays and parametric types
--------------------------------------

This is the array syntax:

    // declaring a variable of an array type
    int#[6] numbers = [1, 1, 2, 3, 5, 8];
    
    // equivalent syntaxes two-dimensional arrays
    int#[2,2] matrix = [[1, 0], [0, 1]];
    int#[2]#[2] same = [[1, 0], [0, 1]];
    
    // accessing elements
    int a = numbers#[0];
    numbers#[1] = 123;
    
    // equivalent
    int b1 = matrix#[0,0];
    int b2 = matrix#[0]#[0];
    
    // equivalent
    int c1 = same#[0,0];
    int c2 = same#[0]#[0];


This is the syntax for type parameters:

    // T must be a completely known type or list[T] won't be completely known
    typedef list[T] = (
        count list_size;
        T[list_size]^ elements;
    );
    
    // T may be an incomplete type. ptrlist[T] will always be completely known
    typedef ptrlist[T] = (
        count list_size;
        T^[list_size]^ pointers;
    );

    list[int] a;
    
    // both work with incomplete types
    () add_to_list[T](T^ elem, ptrlist[T] elements);
    () add_to_list[T](T^ elem, list[T^] elements);

    // covariance/contravariance?