One thing that’s expected of a scripting IDE is code completion, which in PrimalScript we call “PrimalSense.” In an object-oriented language like Windows PowerShell, we didn’t feel it was enough to provide PrimalSense just for the basic cmdlets; we also wanted to provide PrimalSense for variables, since in PowerShell variables are really objects with rich methods and properties you can use. The problem is that, by default, PowerShell treats all variables as a generic Object type, which doesn’t have any really useful type-specific capabilities. It does, however, offer a way to force variables to be treated as a specific type, so that was our key to providing a better editing environment. Take a look:
Here, a variable $regexobj has been set to the [regex] type. By defining the type on the first use of the variable, PrimalSense is able to associate the variable with that type and provide appropriate PrimalSense menus for it throughout the remainder of the script. This will not only work for the type accelerators in PowerShell ([string], [int], and so forth), but also for .NET Framework types, like [System.String], so you can use any data type you wish.
While it might seem inconvenient to have to specifically type your variables this way, it’s actually a good practice: It’ll make your code a bit more self-documenting, and it’ll make debugging easier in a lot of situations (if you try to put a string into an [int] variable, for example, PowerShell will throw an error that’s very clear and easy to understand). Plus, there’s obviously no way PrimalScript could “figure out” what data type you “meant” for a variable to use; by specifically telling it, you’ll get PrimalSense for that data type. And you don’t have to type your variables in PrimalScript; if you don’t, it’ll just assume you want to use the variable as a generic Object and will act accordingly when it comes to PrimalSense.
Check out more at http://www.primalscript.com/psnextpreview.asp.
Technorati Tags: powershell Script Editor Code Completion Variable Types