![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | 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) |
| Guest | 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 | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Annoying quirk... can you help? | anthrorob | Vista performance & maintenance | 2 | 10-04-2007 04:53 AM |
| Typecasting issue | Jonathan | PowerShell | 6 | 04-12-2007 08:26 AM |
| More.com?? Quirk | Andrew Watt [MVP] | PowerShell | 11 | 01-23-2007 12:53 PM |
| Another AD formatting quirk / bug | Andrew Watt [MVP] | PowerShell | 4 | 06-29-2006 05:19 AM |