Skip to content

Commit ff5f67a

Browse files
committed
RFC: Remove ~ in favor of box and Box
1 parent 7443c30 commit ff5f67a

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

active/0000-remove-tilde.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
- Start Date: 2014-04-30
2+
- RFC PR #: (leave this empty)
3+
- Rust Issue #: (leave this empty)
4+
5+
# Summary
6+
7+
The tilde (`~`) operator and type construction do not support allocators and therefore should be removed in favor of the `box` keyword and a language item for the type.
8+
9+
# Motivation
10+
11+
* There will be a unique pointer type in the standard library, `Box<T,A>` where `A` is an allocator. The `~T` type syntax does not allow for custom allocators. Therefore, in order to keep `~T` around while still supporting allocators, we would need to make it an alias for `Box<T,Heap>`. In the spirit of having one way to do things, it seems better to remove `~` entirely as a type notation.
12+
13+
* `~EXPR` and `box EXPR` are duplicate functionality; the former does not support allocators. Again in the spirit of having one and only one way to do things, I would like to remove `~EXPR`.
14+
15+
* Some people think `~` is confusing, as it is less self-documenting than `Box`.
16+
17+
* `~` can encourage people to blindly add sigils attempting to get their code to compile instead of consulting the library documentation.
18+
19+
# Drawbacks
20+
21+
`~T` may be seen as convenient sugar for a common pattern in some situations.
22+
23+
# Detailed design
24+
25+
The `~EXPR` production is removed from the language, and all such uses are converted into `box`.
26+
27+
Add a lang item, `box`. That lang item will be defined in `liballoc` (NB: not `libmetal`/`libmini`, for bare-metal programming) as follows:
28+
29+
#[lang="box"]
30+
pub struct Box<T,A=Heap>(*T);
31+
32+
All parts of the compiler treat instances of `Box<T>` identically to the way it treats `~T` today.
33+
34+
The destructuring form for `Box<T>` will be `box PAT`, as follows:
35+
36+
let box(x) = box(10);
37+
println!("{}", x); // prints 10
38+
39+
# Alternatives
40+
41+
The other possible design here is to keep `~T` as sugar. The impact of doing this would be that a common pattern would be terser, but I would like to not do this for the reasons stated in "Motivation" above.
42+
43+
# Unresolved questions
44+
45+
The allocator design is not yet fully worked out.
46+
47+
It may be possible that unforeseen interactions will appear between the struct nature of `Box<T>` and the built-in nature of `~T` when merged.

0 commit comments

Comments
 (0)