#16add `defer` statements

  • avatar~clovercommented · 2 years ago
    Author
    Owner

    we should copy what zig does


    Milestone: 0.3.0.

  • avatar~clover changed the status to Todo · 2 years ago
  • avatar~clovercommented · 2 years ago
    Author
    Owner

    there are two ways to lower defer blocks

    a();
    defer deferred_action();
    if c { return d(); }
    e();
    
    1. copy pasting
    a();
    if (c) {
      let temp = d();
      deferred_action();
      return temp;
    }
    e();
    deferred_action();
    
    1. try-finally
    a();
    try {
      if (c) { return d(); }
      e();
    } finally {
      deferred_action();
    }
    

    i think there are cases 1 will be preferred over 2, particularly when there is only defer point. try-finally seems to be a smarter approach.