enum Tree<T> {
case Leaf(T)
indirect case Node(Tree, Tree)
}
But with structs, last time I checked, you can not do
struct LinkedList<T> {
var value : T
indirect var tail : LinkedList<T>?
}
This may be tolerable for internal types, but what if you interface with a REST API from another company and some API call can return a type where the json schema is recursive? Do I just have to switch to reference types?
Maybe allowing to annotate optional struct members as indirect would be a cleaner solution in the future.
The naive implementation of this would have to allocate for every new value, FWIW. Presumably you’re imagining Optional<Box<LinkedList>>, but it would actually be Box<Optional<LinkedList>> unless we made an exception for optionals.
The problem with the "wrap the property type in a box" approach is that it forces a separate heap allocation for each indirect property, which is going to increase memory fragmentation. This is fine if you just have a single property, but if you start adding more indirect properties, you're going to want them to be contained in the same "box".
Someone could probably develop a macro-based solution here that would extract the relevant properties into a storage class, but this is something that the language should really support directly because there are subtle inefficiencies/feetguns if you do it wrong (or even do it right): you have to implement copy-on-write yourself if your type is mutable, you have to write forwarding accessors and make sure they don't become a performance bottleneck, the box class adds useless metadata to the binary that you don't actually need/want, etc.