Windows Vista Forums
Vista Forums Home Join Vista Forums Webcasts Windows 7 Forum Vista Tutorials Tags

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.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

quirk/bug of typecasting of types in functions

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 11-15-2006   #1 (permalink)
klumsy@gmail.com
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 SpecsSystem Spec
Old 11-15-2006   #2 (permalink)
Adam Milazzo
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 SpecsSystem Spec
Closed Thread

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


Vistax64.com 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 2005-2008

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 47 48 49 50 51