PowerShell-RFC icon indicating copy to clipboard operation
PowerShell-RFC copied to clipboard

Add using () statement for automatic disposing

Open jborean93 opened this issue 1 year ago • 3 comments

jborean93 avatar Apr 19 '24 11:04 jborean93

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. :-)

essentialexch avatar Apr 19 '24 12:04 essentialexch

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
}

DerpMcDerp avatar May 14 '24 06:05 DerpMcDerp

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

jborean93 avatar May 14 '24 06:05 jborean93