3 ms·
Better enums, but not better ADTs. I didn’t write a lot of compilers in Rust yet, but I find it limiting that you can’t pattern match across a Box<T>, and all
by sshine 3y ago
Better enums, but not better ADTs.
I didn’t write a lot of compilers in Rust yet, but I find it limiting that you can’t pattern match across a Box<T>, and all ASTs require boxing for recursive references.
- noam_k 3y agoI think what you want is to enable box_patterns[0]. I have no idea why it isn't stable yet. [0] https://doc.rust-lang.org/beta/unstable-book/language-features/box-patterns.html https://doc.rust-lang.org/beta/unstable-book/language-featur...
- ForkMeOnTinder 3y agoI believe it's not stable because it gives std::boxed::Box special treatment in the language. They'd rather find a solution that could work equally well for smart pointers defined outside of std. Box patterns are likely to be superseded by one of these: https://github.com/rust-lang/rust/issues/42640 https://github.com/rust-lang/rust/issues/42640 https://github.com/rust-lang/rust/issues/87121 https://github.com/rust-lang/rust/issues/87121
- Georgelemental 3y agoThe Rust language team wants a more general solution that also works with types like `Arc`, instead of hardcoding a special case for `Box` into the language. This is generally called "deref patterns." But there are tricky open design questions, so the effort is stalled until someone comes up with a proposal addressing them
- drexlspivey 3y ago> I didn’t write a lot of compilers in Rust yet what a pleb
- Rusky 3y agoOften, when you have a compiler-y recursive ADT, you will want to allocate the individual nodes in an arena rather than from the global allocator as with Box. And at that point, you can use references (with the lifetime of the arena) rather than Boxes, and you can pattern match across those. (Note that this doesn't work if your "arena" is just a Vec, which is something people have sort of hijacked the term "arena" for. It needs to be an arena that doesn't reallocate its contents on growth, something like https://docs.rs/bumpalo/latest/bumpalo/ https://docs.rs/bumpalo/latest/bumpalo/, in order to hand out references that live across allocations like this.)
- estebank 3y agoI do find myself wanting deref patterns in the rustc codebase often :)
- deleted 3y ago[deleted]