![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | quirk/bug of typecasting of types in functions ok so i want to pass in types to a function anyway to start if i run [int] i get back i type.. i can test it by doing [int] -is [type] or [int].tostring() gives me System.Int32 however i want to pass in types to functions i.e so i make this function function typetest() { $args[0].tostring() $args[0] -is [type] } then call typetest [int] and low and beyond the results are things [int] False it seems the function dispatcher of powershell decided to turn this into a string however if i do typetest $([int]) typetest ([int]) but that is overkill imo. the same with this function function typetest2([type] $thetype) { $thetype.tostring() } it will error out if i call it with typetest2 [int] which is very counter intuitive.. i'd like MS's opinion, thoughts of this. |
My System Specs![]() |
| | #2 (permalink) |
| | Re: quirk/bug of typecasting of types in functions klumsy@gmail.com wrote: > ok so i want to pass in types to a function > > anyway to start > if i run > > [int] > > i get back i type.. i can test it by doing [int] -is [type] or > [int].tostring() gives me System.Int32 > > however i want to pass in types to functions i.e so i make this > function > > function typetest() > { > $args[0].tostring() > $args[0] -is [type] > } > > then call > > typetest [int] > > and low and beyond the results are things > > [int] > False > > it seems the function dispatcher of powershell decided to turn this > into a string If you don't specify a type for the arguments, they will be passed without conversion. Ie, you entered [int] as a string, so it was passed as a string. > however if i do > > typetest $([int]) > typetest ([int]) Yeah, PowerShell has two "parsing modes" -- command mode and expression mode. In expression mode, it operates more like a programming language. In command mode, it operates more like a shell. You can check the docs to see exactly when PowerShell uses each mode, but basically, you entered something that looks like a command, so it was parsed in command mode. In command mode, all arguments are strings by default. Using $() or () causes a recursion in the parser, so you can switch between command and expression mode this way. So when you entered: PS> [int] It looks like an expression so it was parsed as an expression. When you entered: PS> typetest [int] It looks like a command so it was parsed as a command. > but that is overkill imo. > > the same with this function > > function typetest2([type] $thetype) > { > $thetype.tostring() > } > > it will error out if i call it with > > typetest2 [int] > > which is very counter intuitive.. i'd like MS's opinion, thoughts of > this. The automatic conversion to type wants the type's name. The name of the int type is "int" (or Int32 or System.Int32). The square brackets are not part of the name. So if you try the following, it will work as expected: PS> typetest2 int Check out the documentation where it describes the parsing modes. Hopefully it will make things clearer. |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Attachments quirk | Vista mail | |||
| Printer Quirk | Vista hardware & devices | |||
| Typecasting issue | PowerShell | |||
| More.com?? Quirk | PowerShell | |||