Woah, I almost wrote a similar blog post! Some thoughts, mostly coming from the difference of what I was going to write.
Please don't call it
capability. This proposal is for a data sharing mechanism, whereas capabilities are unforgeable access-granting objects. This data sharing mechanism would be useful for sharing capabilities, but it's also useful for sharing other data, and capabilities could be shared with this or in other ways. People learn words through examples, and I really don't want to try to explain to people "no, Rust doesn't have capabilities by default, unless you use the cap-std crate, oh you're talking about the things literally called
capability... no those are unrelated". In my not-yet-written blog post, I was going to call it
shared. Some other possibilities:
I was picturing a different implementation: each context would be shared in a global (
static) variable. It would be uninitialized memory to start, and set by the
with expression. But I've been trying to translate your example to this style and failing; there's no place for the lifetime
'a of the arena to go. So I don't think this works, and the implementation needs to be passing a function argument down instead (which I think is what the post is proposing, though I'm not entirely sure?). It seems strange to me, though: there can only be one
basic_arena at once, right? Shouldn't it be possible to store it in a single global location?
For making it more economical to use contexts all over the place, I imagined being able to annotate an module with a
with clause, which would effectively put a
with clause on every top-level function it defined. This would be useful in a big codebase where 90% of the code wants to share a s
If you write
context alloc: Allocator;, where
Allocator is a trait, is there going to be a v-table somewhere? If so, I would think the Rusty thing to do would be to be explicit, and simply state the type of the actual value being passed around:
context alloc = Box<dyn Allocator> or whatever.
Anyhow, I never actually wrote that post because I hadn't figured out the details, or a concrete proposal that would work. So take this all with a grain of salt. Hopefully at least useful in a brainstorming sort of way.