PowerShell / PowerShell-RFC

RFC (Request for Comments) documents for community feedback on design changes and improvements to PowerShell ecosystem
MIT License
423 stars 121 forks source link

Add using () statement for automatic disposing #368

Open jborean93 opened 2 months ago

jborean93 commented 2 months ago
essentialexch commented 2 months ago

Dave Wyatt wrote a using-object in 2014. I still use it heavily: https://davewyatt.wordpress.com/2014/04/11/using-object-powershell-version-of-cs-using-statement/ - but yes, it suffers from some of the challenges you mention and isn't as flexible. I would just ask that you don't break it. :-)

DerpMcDerp commented 1 month ago

This proposal would be more useful if there was a way for the using in the begin block to be Dispose()d in the end block. i.e.

begin {
    using $a = whatever
    ...
}
process {
    ...
}
end {
    ...
    # $a gets Dispose()d here
}
jborean93 commented 1 month ago

@DerpMcDerp that’s what the clean block added in PowerShell 7.3 is great for!