aboutsummaryrefslogtreecommitdiff
path: root/docs/notes/userdef_struct_compat.txt
blob: a543e09d3bfdfc3793d55926bf41a46dee3ff990 (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
50
51
52
53
54
55
56
57
58
59
60

user-defined "structure" types
------------------------------

The types (int x, int y) and Point, or int and Id, might be compatible types.
However, it would be nice if types like String and Filename could be
compatible as well. There are some solutions:

(This is the is-a/has-a problem)

1) add a typedef qualifier to the base type (i.e. String):

    // the "struct" keyword is a bit confusing
    typedef struct String = byte^;
    typedef Filename = String;
    
    typedef structcompat String = byte^;
    typedef Filename = String;
    
    // pros:
    // Same behaviour for e.g. String and int
    //
    // Cons:
    // can't create string subtypes that are not struct compatible
    

2) add a typedef qualifier to the subtype (i.e. Filename):

    typedef String = byte^;
    typedef structcompat Filename = String;

    typedef String = byte^;
    typedef structcompat Filename = String;

3) use the existing "alias" qualifier:

    typedef struct String = byte^;
    typedef alias Filename = String;

4) use "is" instead of "=":

    typedef String is byte^;
    typedef Filename is String;
    
    // should it be possible to go two steps, e.g. from byte^ to Filename?

5) don't use this at all. The user has to use the "as" keyword (and make "as"
   always works on structurally compatible types?)


    typedef String = byte^;
    typedef Filename = String;
    
    () test() {
        String s;
        Filename f = s; // what to do here???
    }