3 ms·
One of my favorite parts of Powershell is optional typing. Variables are a generic "Object" type by default, which can hold anything from a string to array to "
by stephengillie 9y ago
One of my favorite parts of Powershell is optional typing. Variables are a generic "Object" type by default, which can hold anything from a string to array to "Amazon.AWS.Model.EC2.Tag" or other custom types.
Or, type can be specified when setting the variable:
[String]$myString = "Hello World!"
This would generate a type error:
[Int]$myString = "Hello World!"
Often, typed and untyped variables will sit together:
[Int]$EmployeeID,[String]$FullName,$Address = $Input -split ","
- GenericsMotors 9y agoIndeed! I think one of my favourites has to be: [xml]$someXmlDocument = Get-Content "path\to\file.xml" And you get a deserialized version of the XML text. Also the fact that you can use types when declaring function arguments, removing the need to manually test if an object of the desired type was passed. Powershell definitely strikes a good balance on type safety for a scripting language.
- amelius 9y agoSo when you call a function which takes a String as argument, do you need to cast the value manually? If no, then what is the use of the typesystem? If yes, isn't that cumbersome, since I suppose most library functions have typed arguments?