-
Notifications
You must be signed in to change notification settings - Fork 7.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PS implementation of the C# Using statement #9886
Comments
Come to think of it, I think this has come up in discussions previously, where the idea that this could actually be a cmdlet as well surfaced. Compiled cmdlets already have the capability to properly implement disposable features, after all. Do you think a cmdlet would be better than a keyword? It might be possible to have it be pipeline-capable if it's a cmdlet, which could be interesting, depending on implementation, and cmdlets are a little more flexible than keywords... Also, if it's a cmdlet we could package it in a separate module and put it on the PSGallery, too, so even PS5.1 could make use of it as well. 🤔 |
An interesting theory creating as a cmdlet. I'm still feeling its nicer if a keyword then it would be part of the language and get utilized more. |
I think that makes sense. We do have the unfortunate consequence there of it being unavailable on downlevel powershell versions, though. Also, keywords tend to be a bit less discoverable than cmdlets in general; keywords don't tab-complete, but functions and cmdlets do, and they're a bit easier to find in the help documents as well -- most language features are documented in a slew of I'm not against a keyword, despite all that. Keywords have a really nice no-nonsense simplicity about them that can be really nice to work with. Cmdlets can tend towards feature creep, among other things, and tend to be a bit slower to operate. I'm not really sure which approach is really better. Either way, though, we should have this implemented in some form or another. 😄 |
I think there is a good argument for both to be fair. I do get the backward compatibility but maybe it also encourages the use of PS 7 with shiny new features! Interesting to hear other peoples thoughts on this as well. |
It would be difficult to do Last time this came up, @BrucePay suggested this syntax: Use-Object { [SomeDisposable]::new() } {
$PSItem.DoSomethingWithDisposable()
} That could work, but I'd still be worried that a pipeline stop would be triggered before the cmdlet finished invoking the script block (that's assuming that
eh... if you know enough to know that something needs to be disposed, you probably know about |
@Graham-Beer The |
That sums it up nicely for me as well. |
Does a As for the lack of sustained interest... I think the lack of decent here's an example from an earlier issue: function Get-RemoteCertificate {
[CmdletBinding()]
[OutputType([System.Security.Cryptography.X509Certificates.X509Certificate])]
param (
[Parameter(
Mandatory,
ValueFromPipeline
)]
[ValidateNotNull()]
[Uri]
$Uri
)
process {
try {
$TcpClient = [System.Net.Sockets.TcpClient]::new($Uri.Host, $Uri.Port)
try {
$SslStream = [System.Net.Security.SslStream]::new($TcpClient.GetStream())
$SslStream.AuthenticateAsClient($Uri.Host)
$SslStream.RemoteCertificate
} finally {
$SslStream.Dispose()
}
} finally {
$TcpClient.Dispose()
}
}
} |
Yes, with some caveats. If you place the assignment inside the So I've taken to a pattern like this: $client = $null
try {
$client = [TcpClient]::new($uri.Host, $uri.Port)
} finally {
if ($null -ne $client) {
$client.Dispose()
}
} It's not pretty, but it's safe. |
oof... that's even more painful. |
I have a PR in the works (#9900) to make this much easier for pipeline cmdlets (esp. those that need to keep the object around for the entire That's honestly one pretty clear case for a using keyword, I must say... |
If only we had |
I quite like the C# syntax of ?, @rkeithhill. How do we push this topic idea forward? |
I think there may be an existing issue for it, but if not create one -- if there already is one, add a comment. |
If there is an open issue for the |
Any time one ends up in the world of .net objects not having clean disposal is a pain. This is especially bad for file manipulation when file handles don't get disposed of. I needed to get the file entries in a zip file and resorting to system.io.compression and streams results in lots of ugly try catch finally and disposing |
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
2 similar comments
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes. |
Summary of the new feature/enhancement
The C# language provides a convenient syntax that ensures the correct use of IDisposable objects. I believe this would be a great addition to the PowerShell language, as the 'Using statement' simplifies the code that you have to write to create a resource and then finally clean up the object.
Without the using statement you are required to ensure that Dispose() is called, then followed by the Close() method. By implementing the 'Using statement' you can assume that all kinds of streams are getting closed.
I would like to see the PowerShell language use a similar syntax to C#, which would be
using (expression) statement
.PowerShell does make use of the
$using
variable, but I don't believe adding the 'using' keyword will cause any issues.The text was updated successfully, but these errors were encountered: