rfcs icon indicating copy to clipboard operation
rfcs copied to clipboard

Closure-level inline constants

Open sojuz151 opened this issue 1 year ago • 3 comments

I propose to add closure-level inline constants. For example the following code

let closeure_uuid = Uuid::new_v4();
let lambda = ||  {
println!("Execution UUID {}",Uuid::new_v4());
println!("Closure UUID {}",closeure_uuid);
};
lambda ();
lambda ();

It could be rewritten as

let lambda = ||  {
println!("Execution UUID {}",Uuid::new_v4());
println!("Closure UUID {}",cc{Uuid::new_v4())});
};
lambda ();
lambda ();

with a new cc keyword similar to constant. This would have similar advantages as inline_const https://github.com/rust-lang/rust/pull/104087. Use cases:

Finer control on what is getting moved inside the lambda. For example

cc{very_larnge_vector.iter().min()}

or

cc{struct.field.other_filed}

More ergonomic dealing with ref counting. Example:

cc{button.clone()}.enable();

Those expressions should be executed when the closure is created and dropped when the closure is dropped.

sojuz151 avatar Sep 02 '24 11:09 sojuz151

I don't understand the proposal tbh, is this just an abbreviated syntax for const {}?

workingjubilee avatar Sep 24 '24 19:09 workingjubilee

this is like one of the capturing alternatives proposed in https://github.com/rust-lang/rfcs/pull/3680#issuecomment-2308559508.

as explained in https://github.com/rust-lang/rfcs/pull/3680#issuecomment-2328377258 it is very hard to grasp the scope/lifetime of the cc {}'ed expression for newcomers.

kennytm avatar Sep 24 '24 19:09 kennytm

ah, I see. thank you.

workingjubilee avatar Sep 24 '24 20:09 workingjubilee