[cdd1695] | 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 |
---|
[974bcdd] | 5 | conversion that may recive language level support or even be implicit. |
---|
[cdd1695] | 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 | Tags cannot be used on unions. This is because the different sides interact |
---|
| 14 | with the casts rather badly. There is a similarity with tagged unions still. |
---|
| 15 | Tagged structures also carry data to identify which out of several |
---|
| 16 | possibilies the object actually is. Although the possibilies are dynamic in |
---|
| 17 | this case. |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | Syntax: |
---|
| 21 | |
---|
| 22 | "struct" name [ "tagged" [ parent-name ] ] "{" fields "}" |
---|
| 23 | |
---|
| 24 | The keywords can change (although they currently reflect the concept name |
---|
[974bcdd] | 25 | closely). More formally, in terms of grammar this adds: |
---|
[cdd1695] | 26 | |
---|
| 27 | struct-or-union-specifier |
---|
| 28 | ... |
---|
| 29 | struct identifier tagged { struct-declaration-list } |
---|
| 30 | struct identifier tagged parent-identifier { struct-declaration-list } |
---|
| 31 | |
---|
| 32 | "tagged" by itself create a tagged structure that is the root of a new tree. |
---|
| 33 | It has no parent tagged structure. If "tagged" is used with a parent than |
---|
| 34 | that is the parent of this node. |
---|
| 35 | |
---|
| 36 | Tagged structures have fields beyond the ones listed. Root tags have a type |
---|
| 37 | field added which give the type of the instance. Child tags prepend all of |
---|
| 38 | their parent's fields to their field list so they can be upcast. |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | Implemenation: |
---|
| 42 | |
---|
| 43 | Adding to the field list is a simple matter, should be doable during |
---|
| 44 | translation. The type field is just a pointer to a type object. With proper |
---|
| 45 | linking we can create a single unique instance of the type object for each |
---|
| 46 | declared tagged struct. The instance's address is used as an id for the type. |
---|
| 47 | It also holds data about the type, such as its parent's id/a pointer to the |
---|
| 48 | parent type object. |
---|
| 49 | |
---|
| 50 | If the type field is given a simple name, then the user can easily access the |
---|
| 51 | type object. This might be useful depending on what sort of data is in the |
---|
| 52 | type object, especially if the data can be added to by the user in some way. |
---|
| 53 | Ironically one way to accomplish that is to make the type objects tagged |
---|
| 54 | themselves, but that recursion might not have a base case. |
---|
| 55 | |
---|
| 56 | If the name is mangled and direct access to type objects is still wanted, then |
---|
| 57 | a function could be used to access the type object. Say get_type or get_tag |
---|
| 58 | instead of type or tag. |
---|
| 59 | |
---|
| 60 | If the data on the type object is set, than providing direct access may be |
---|
| 61 | unnessary. Instead the libraries or base code might be able to implement |
---|
| 62 | everything the data is for. |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | Traits: |
---|
| 66 | |
---|
| 67 | [is_]tagged[_struct](dtype T) |
---|
| 68 | True if the given T is a tagged struct of some kind. This promises that it has |
---|
| 69 | a type object, but nothing else. |
---|
| 70 | |
---|
| 71 | [is_]tagged_under(dtype parent, dtype child) |
---|
| 72 | True if child is a child type of parent. Requires that both are tagged structs |
---|
| 73 | and that child can upcast to parent. |
---|
| 74 | |
---|
| 75 | |
---|
| 76 | Functions: |
---|
| 77 | |
---|
| 78 | forall(dtype T | is_tagged(T), dtype U | is_tagged(U)) |
---|
| 79 | T * dynamic_cast(U * value) |
---|
| 80 | The cast function, that safely converts the U* into a T*, returning null if |
---|
| 81 | the underlying object value points to is not a child type of T. A shorter name |
---|
| 82 | might be perfered. The runtime should be no more than linear with the depth |
---|
| 83 | of U in the inhiertance tree. |
---|
| 84 | |
---|
| 85 | bug#11 might require `bool dynamic_cast(T ** dst, U * src)` instead. |
---|