cup icon indicating copy to clipboard operation
cup copied to clipboard

[Idea] `with` blocks instead of `defer`

Open Weltspear opened this issue 3 years ago • 6 comments

Maybe instead of using defer statements we can use with blocks:

struct A{
    a: i32,
}

fn new_a(): A*{
    ...
}

method A::close(){
    ...
}

fn main(){
    with let foo: A* = new_a(){
        ...
    }
}

Which essentially means:

struct A{
    a: i32,
}

fn new_a(): A*{
    ...
}

method A::close(){
    ...
}

fn main(){
    let foo: A* = new_a();
    ...
    foo::close();
}

Weltspear avatar Jul 28 '22 13:07 Weltspear

I've considered this, but I'm not really sure how much I like the proposed idea. The problem for me is really that this forces everything to be indented one level, which I'm not a fan of. So, if I were to open 3 files at once, I would have 3 nested with blocks and code keeps moving to the right.

If you have any ideas on how we can avoid this, maybe there's another variation of this idea we can add.

mustafaquraish avatar Jul 28 '22 22:07 mustafaquraish

Instead of doing nested blocks we can do something like this:

with let a = ..., let b = ... {
    ...
}

Weltspear avatar Jul 29 '22 07:07 Weltspear

Instead of doing nested blocks we can do something like this:

Sounds reasonable. I have nothing against this, but I would prefer if we had these alongside defer instead of replacing it.

mustafaquraish avatar Jul 29 '22 17:07 mustafaquraish

Okay

Weltspear avatar Jul 29 '22 17:07 Weltspear

We can implement those as syntatic sugar to defer basically. Does defer support multiple defers per block?

Weltspear avatar Jul 29 '22 17:07 Weltspear

We can implement those as syntatic sugar to defer basically. Does defer support multiple defers per block?

Yep, you can have multiple defers in a block.

mustafaquraish avatar Jul 29 '22 19:07 mustafaquraish