Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closure-level inline constants #3687

Open
sojuz151 opened this issue Sep 2, 2024 · 3 comments
Open

Closure-level inline constants #3687

sojuz151 opened this issue Sep 2, 2024 · 3 comments

Comments

@sojuz151
Copy link

sojuz151 commented Sep 2, 2024

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 rust-lang/rust#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.

@workingjubilee
Copy link
Member

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

@kennytm
Copy link
Member

kennytm commented Sep 24, 2024

this is like one of the capturing alternatives proposed in #3680 (comment).

as explained in #3680 (comment) it is very hard to grasp the scope/lifetime of the cc {}'ed expression for newcomers.

@workingjubilee
Copy link
Member

ah, I see. thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants