source: doc/proposals/tagged-struct.txt @ 1a42132

Last change on this file since 1a42132 was 1a42132, checked in by Andrew Beach <ajbeach@…>, 4 years ago

Added Aaron's comments to the tagged-struct proposal.

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