-
Notifications
You must be signed in to change notification settings - Fork 1.7k
How box should work #413
Copy link
Copy link
Closed
Labels
A-allocationProposals relating to allocation.Proposals relating to allocation.A-expressionsTerm language related proposals & ideasTerm language related proposals & ideasA-operatorOperators related proposals.Operators related proposals.A-placement-newProposals relating to placement new / box expressions.Proposals relating to placement new / box expressions.A-traits-libstdStandard library trait related proposals & ideasStandard library trait related proposals & ideasA-uninit&uninit related proposals & ideas&uninit related proposals & ideasT-langRelevant to the language team, which will review and decide on the RFC.Relevant to the language team, which will review and decide on the RFC.
Metadata
Metadata
Assignees
Labels
A-allocationProposals relating to allocation.Proposals relating to allocation.A-expressionsTerm language related proposals & ideasTerm language related proposals & ideasA-operatorOperators related proposals.Operators related proposals.A-placement-newProposals relating to placement new / box expressions.Proposals relating to placement new / box expressions.A-traits-libstdStandard library trait related proposals & ideasStandard library trait related proposals & ideasA-uninit&uninit related proposals & ideas&uninit related proposals & ideasT-langRelevant to the language team, which will review and decide on the RFC.Relevant to the language team, which will review and decide on the RFC.
Type
Fields
Give feedbackNo fields configured for issues without a type.
We need to come up with a final design for how the
boxoperator should work.Here is what I personally think would be ideal; unfortunately it involves features which are not yet part of the language.
The
boxoperator would desugar to a call tomake_box(see below). If a value-level argument is required to specify where the box should be allocated, such as for arenas (i.e.box(here) valsyntax), its type is specified by theWhereassociated type. Most of the time this defaults to().MakeBoxitself is a higher-kinded trait, implemented for the generic box type itself. (So if you haveimpl MakeBox for SomeBox,val: T, then e.g.box val: SomeBox<T>.)make_boxrelies on&outreferences such as described by RFC PR 98 (see also the comments), which is a reference type which points to an uninitialized value and represents the obligation to initialize it.make_boxtakes as argument the obligation to initialize a box, e.g. aSomeBox<T>, allocates the box itself, and returns to the caller the obligation to initialize the contained value.We would have
impls such as:Given an expression:
from_string()).SomeBox<Foo>, wherefoo: Foo,SomeBox: MakeBox, andhere: <SomeBox as MakeBox>::Where.hereis omitted, it defaults to(), sobox foobecomesbox(()) foo.Box.let rc_foo: Rc<_> = box foo;. When we gain general type ascription syntax, it could instead bebox foo: Rc<_>. (In each case the result type isRc<Foo>.)This latter point is why it is important for the trait to be higher-kinded. If the trait is higher-kinded, it is sufficient to annotate the outer type (the box type), and the type argument can be inferred, because it is the same as the type of the value being
boxed. IfMakeBoxwere formulated using an associated type instead, as e.g.Deref, then the type ascription would be required to specify the full type, including the type of the contained value.Finally, given the above expression:
This would desugar to:
The second line initializes
the_boxusingmake_box, and then initializes the contained value withfoo. (foohere is an arbitrary expression, not necessarily a variable.)