{"id":4361,"date":"2009-03-11T19:53:00","date_gmt":"2009-03-11T19:53:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2009\/03\/11\/reserving-keywords\/"},"modified":"2019-02-18T13:12:46","modified_gmt":"2019-02-18T20:12:46","slug":"reserving-keywords","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/reserving-keywords\/","title":{"rendered":"Reserving keywords"},"content":{"rendered":"<p>A couple months ago we asked our MVPs how they felt about reserving keywords in V2.&nbsp; We received some excellent but mixed feedback, so we\u2019d like to open the discussion up a bit wider.<\/p>\n<p>There are keywords we hope to use in a future version of PowerShell, but won\u2019t be implemented in V2.&nbsp; \u201cclass\u201d, \u201cusing\u201d, and \u201cnew\u201d are excellent candidates for additions to PowerShell, but we haven\u2019t decided on the exact list yet.<\/p>\n<p>Because V1 did not reserve these keywords, reserving them now would be a breaking change.&nbsp; For example, some people like to add an alias like:<\/p>\n<pre class=\"PowerShellColorizedScript\"><span>New-Alias<\/span> <span>-Name<\/span> <span>new<\/span> <span>-Value<\/span> <span>New-Object<\/span><\/pre>\n<p>If we reserved \u201cnew\u201d, scripts using this alias would be broken.&nbsp; Similarly, in Bruce Payette\u2019s book \u201cWindows PowerShell In Action\u201d, he describes a way to implement a keyword like \u201cclass\u201d, though he uses \u201cCustomClass\u201d instead of \u201cclass\u201d<\/p>\n<p>The biggest benefit of reserving in V2 is that we\u2019ll break fewer scripts.<\/p>\n<p>The cons to reserving in V2 include:<\/p>\n<ul>\n<li>Breaking scripts needlessly.&nbsp; We\u2019ve tried to make V2 as backwards compatible as possible.&nbsp; The breaking changes that we do have implement real functionality (for example, try\/catch are new keywords, breaking scripts that used try or catch as a function.) <\/li>\n<li>Some keywords can be implemented in script, at least well enough to look like a real keyword. <\/li>\n<\/ul>\n<p>Here\u2019s an example of how you might implement the C# using statement in a V2 PowerShell script:<\/p>\n<pre class=\"PowerShellColorizedScript\"><span>function<\/span> <span>using<\/span>\n<span>{<\/span>\n    <span>param<\/span><span>(<\/span><span>$obj<\/span><span>,<\/span> <span>[scriptblock]<\/span><span>$sb<\/span><span>)<\/span>\n    <span>try<\/span> <span>{<\/span>\n        <span>&amp;<\/span> <span>$sb<\/span>\n    <span>}<\/span> <span>finally<\/span> <span>{<\/span>\n        <span>if<\/span> <span>(<\/span><span>$obj<\/span> <span>-is<\/span> <span>[IDisposable]<\/span><span>)<\/span> <span>{<\/span>\n            <span>$obj<\/span><span>.<\/span><span>Dispose<\/span><span>(<\/span><span>)<\/span>\n        <span>}<\/span>\n    <span>}<\/span>\n<span>}<\/span>            <\/pre>\n<p>And here is how you can use that function \u2013 it looks very similar to how a using keyword might look:<\/p>\n<pre class=\"PowerShellColorizedScript\"><span>using<\/span> <span>(<\/span><span>$stream<\/span> <span>=<\/span> <span>new-object<\/span> <span>System.IO.StreamReader<\/span> <span>$PSHOME\\types.ps1xml<\/span><span>)<\/span> <span>{<\/span>\n    <span>foreach<\/span> <span>(<\/span><span>$_<\/span> <span>in<\/span> <span>1<\/span><span>..<\/span><span>5<\/span><span>)<\/span> <span>{<\/span> <span>$stream<\/span><span>.<\/span><span>ReadLine<\/span><span>(<\/span><span>)<\/span> <span>}<\/span>\n<span>}<\/span><\/pre>\n<p>Feel free to give us your feedback in the comments \u2013 are you using any common language keywords as a function name in your scripts?&nbsp; If you do, how bothersome would it be if you had to change your scripts?<\/p>\n<p>&#8211; Jason Shirk [MSFT]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A couple months ago we asked our MVPs how they felt about reserving keywords in V2.&nbsp; We received some excellent but mixed feedback, so we\u2019d like to open the discussion up a bit wider. There are keywords we hope to use in a future version of PowerShell, but won\u2019t be implemented in V2.&nbsp; \u201cclass\u201d, \u201cusing\u201d, [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4361","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>A couple months ago we asked our MVPs how they felt about reserving keywords in V2.&nbsp; We received some excellent but mixed feedback, so we\u2019d like to open the discussion up a bit wider. There are keywords we hope to use in a future version of PowerShell, but won\u2019t be implemented in V2.&nbsp; \u201cclass\u201d, \u201cusing\u201d, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/4361","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=4361"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/4361\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=4361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=4361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=4361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}