Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - quirk/bug of typecasting of types in functions

Reply
 
Old 11-15-2006   #1 (permalink)
klumsy@gmail.com


 
 

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 SpecsSystem Spec
Old 11-15-2006   #2 (permalink)
Adam Milazzo


 
 

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 SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Attachments quirk Vista mail
Printer Quirk Vista hardware & devices
Typecasting issue PowerShell
More.com?? Quirk PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46