Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Yeah, as someone else has pointed out it's C# inspired, this is a C# example:

    public void AMethod() {
        //some code
        using var stream = thing.GetStream();
        //some other code
        var x = thing.ReadToEnd();
        //file will be automatically disposed as this is the last time file is used
        //some more code not using file
    } //any error means file will be disposed if initialized
You can still do the wrap if you need more fine grained control, or do anything else in the finally.

You can even nest them like this:

    using var conn = new SqlConnection(connString);
    using var cmd = new SqlCommand(cmd);
    conn.Open();
    cmd.ExecuteSql();

Edit: hadn't read the whole article, the javascript version is pretty good!


Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: