-
-
Notifications
You must be signed in to change notification settings - Fork 220
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
Defer attributes #1829
Comments
This is clearly a very rough idea. This is more intended for resource handling than memory management. Here would be a way to do fn void test()
{
mem::temp_push() @defer(pop);
void* t = tmalloc();
} Which then would replace: fn void test()
{
@pool()
{
void* t = tmalloc();
};
} |
However, this is not the only way to do "flat" fn void test()
{
mem::@temp_push_pop();
void* t = tmalloc();
} So it's not necessarily the simplest way of doing this. Similarly, we can have a file with close: File! f = file::@open_with_autoclose(); |
The downside of macros that insert defers is that this hidden control flow can be hard to understand, even compared to lazy parameters, this s why |
Allowing macros to insert defer into parent scope feels like opening a big can of worms. But I think there should be something in this direction. One example I've found when annotating code for profilers, when you want a scope to be profiled, inserting a single line |
As we discussed on Discord, the proposal of Python
|
Ah forgot, multiple expressions in with too: with(
f: file::open("file.txt", "r")!,
another: file::open("file.txt", "r")!,
mut.lock()
) {
f.write(stuff);
} |
Maybe one could require a double fn Zone @@zone(String name) @defer(zone_end) {
// ...
}
fn zone_end(Zone zone) {
// ...
}
fn void foo() {
profiler::@@zone("foo");
} One could also have macro Zone @@zone(String name) {
$if env::PROFILE:
Zone zone = zone_begin(name);
defer zone_end(zone);
$endif
@body();
} |
This is more of a "let's see if this idea is worth looking at" issue.
The idea can be implemented in several ways, here is one:
The code above implicitly becomes:
We might instead envision it like a method call – this is more appropriate if we think invocation will be done as a method call:
It could also be more complex, maybe you'd define something like:
The text was updated successfully, but these errors were encountered: