1 | Proposal to add simple inhieritance to the language. |
---|
2 | |
---|
3 | Tagged structures allow for dynamic casting between types in a hierarchy. |
---|
4 | Children (rather pointers to) can be up-cast to their parents, a safe |
---|
5 | conversion that may recive language level support or even be implicit. |
---|
6 | Parents can be down cast to their children, which might fail if the underlying |
---|
7 | object is not of the child type, or a child of that. |
---|
8 | |
---|
9 | This does not however cause dynamic look-up. During function calls the |
---|
10 | underlying type is ignored, and the pointer type is used to type match the |
---|
11 | function call. |
---|
12 | |
---|
13 | The name tagged structure comes from tagged union, which carries a value to |
---|
14 | say which of the possible values is currently stored in the union. The idea |
---|
15 | here is similar, however the possibilities are more open ended. |
---|
16 | |
---|
17 | |
---|
18 | Syntax: |
---|
19 | |
---|
20 | "struct" name [ "tagged" [ parent-name ] ] "{" fields "}" |
---|
21 | |
---|
22 | The keywords can change (although they currently reflect the concept name |
---|
23 | closely). More formally, in terms of grammar this adds: |
---|
24 | |
---|
25 | struct-or-union-specifier |
---|
26 | ... |
---|
27 | struct identifier tagged { struct-declaration-list } |
---|
28 | struct identifier tagged parent-identifier { struct-declaration-list } |
---|
29 | |
---|
30 | "tagged" by itself create a tagged structure that is the root of a new tree. |
---|
31 | It has no parent tagged structure. If "tagged" is used with a parent than |
---|
32 | that is the parent of this node. |
---|
33 | |
---|
34 | Tagged structures have fields beyond the ones listed. Root tags have a type |
---|
35 | field added which give the type of the instance. Child tags prepend all of |
---|
36 | their parent's fields to their field list so they can be upcast. |
---|
37 | |
---|
38 | |
---|
39 | Implemenation: |
---|
40 | |
---|
41 | Adding to the field list is a simple matter, should be doable during |
---|
42 | translation. The type field is just a pointer to a type object. With proper |
---|
43 | linking we can create a single unique instance of the type object for each |
---|
44 | declared tagged struct. The instance's address is used as an id for the type. |
---|
45 | It also holds data about the type, such as its parent's id/a pointer to the |
---|
46 | parent type object. |
---|
47 | |
---|
48 | The type field could be hidden (as best as C can hide it) or it could be |
---|
49 | visible to the user with easy access to allow the user to examine the type |
---|
50 | object directly. |
---|
51 | |
---|
52 | Direct access is more useful if the data on the type-objects can change, other |
---|
53 | wise the build in function could handle all cases. Perhaps each root object |
---|
54 | can specify a type object to use or the type objects are themselves tagged, |
---|
55 | although there may not be a base case with the latter. |
---|
56 | |
---|
57 | In the simplest case the type object is a pointer to the parent type object. |
---|
58 | Additional data could be added, such as a name, or a function pointer to the |
---|
59 | destructor. |
---|
60 | |
---|
61 | |
---|
62 | Traits: |
---|
63 | |
---|
64 | [is_]tagged[_struct](dtype T) |
---|
65 | True if the given T is a tagged struct of some kind. This promises that it has |
---|
66 | a type object, but nothing else. |
---|
67 | |
---|
68 | [is_]tagged_under(dtype parent, dtype child) |
---|
69 | True if child is a child type of parent. Requires that both are tagged structs |
---|
70 | and that child can upcast to parent. |
---|
71 | |
---|
72 | |
---|
73 | Functions: |
---|
74 | |
---|
75 | forall(dtype T | is_tagged(T), dtype U | is_tagged(U)) |
---|
76 | T * dynamic_cast(U * value) |
---|
77 | The cast function, that safely converts the U* into a T*, returning null if |
---|
78 | the underlying object value points to is not a child type of T. A shorter name |
---|
79 | might be perfered. The runtime should be no more than linear with the depth |
---|
80 | of U in the inhiertance tree. |
---|
81 | |
---|
82 | bug#11 might require `bool dynamic_cast(T ** dst, U * src)` instead. |
---|
83 | |
---|
84 | |
---|
85 | Tagging Unions (Extention): |
---|
86 | |
---|
87 | Using this system as is does not really work if used on unions directly. |
---|
88 | No new options to the union can be added, as they must be able to upcast. |
---|
89 | Similarly, if options are removed, writing to an upcast union is invalid. |
---|
90 | To allow for growth each option would have to be a structure itself. |
---|
91 | |
---|
92 | Which brings us to "tagget struct union", ie. a union of tagged structures |
---|
93 | as opposed to tagging the union itself. This extention acts as a constraint. |
---|
94 | If unions are declared tagged instead of creating a new tagged type, all |
---|
95 | possible values of the union must be of that tagged type or a child type. |
---|